From 2c7e454ca85b2fbf2f45b01f98b92d36e770c2b7 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 20 Aug 2012 20:13:41 -0400 Subject: [PATCH] add a simple Map class and draw it from the Client --- SConstruct | 4 ++-- src/client/Client.cc | 57 ++++++++++++++++++++++++++++++++------------ src/client/Client.h | 3 +++ src/common/Map.cc | 16 +++++++++++++ src/common/Map.h | 20 ++++++++++++++++ 5 files changed, 83 insertions(+), 17 deletions(-) create mode 100755 src/common/Map.cc create mode 100755 src/common/Map.h diff --git a/SConstruct b/SConstruct index 8cc4898..1b262c8 100755 --- a/SConstruct +++ b/SConstruct @@ -31,14 +31,14 @@ if platform == 'windows': CXX = 'i686-pc-mingw32-g++' MINGW_DIR = '/usr/i686-pc-mingw32/sys-root/mingw/bin' LIBS_client += ['sfml-graphics-s', 'sfml-window-s', 'sfml-system-s', - 'sfml-network-s', 'opengl32', 'mingw32'] + 'sfml-network-s', 'opengl32', 'glu32', 'mingw32'] LIBS_server += ['sfml-network-s', 'mingw32'] LINKFLAGS.append('-static-libstdc++') libs_to_copy.append('%s/libgcc_s_dw2-1.dll' % MINGW_DIR) CPPFLAGS.append('-DSFML_STATIC') else: LIBS_client += ['sfml-network', 'sfml-window', 'sfml-graphics', - 'sfml-system', 'GL'] + 'sfml-system', 'GL', 'GLU'] LIBS_server += ['sfml-network'] LINKFLAGS.append('-Wl,-R%s/lib' % SFML_PATH) diff --git a/src/client/Client.cc b/src/client/Client.cc index 415ac79..5202498 100755 --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -47,20 +47,7 @@ void Client::run() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - /* simple fixed-functionality drawing demo for now */ - glPushMatrix(); - glRotatef(m_clock.getElapsedTime().asSeconds() * 180.0f, 0, 0, 1); - glBegin(GL_QUADS); - glColor3f(1, 0.6, 0.1); - glVertex2f(1, 1); - glColor3f(0, 0, 1); - glVertex2f(-1, 1); - glColor3f(1, 0, 0); - glVertex2f(-1, -1); - glColor3f(0, 1, 0); - glVertex2f(1, -1); - glEnd(); - glPopMatrix(); + draw_map(); m_window->display(); } @@ -78,6 +65,46 @@ void Client::resize_window(int width, int height) float aspect = (float)width / (float)height; glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glOrtho(-1.2 * aspect, 1.2 * aspect, -1.2, 1.2, 1, -1); + gluPerspective(60.0f, aspect, 0.01, 1000.0); glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glRotatef(-70, 1, 0, 0); +} + +void Client::draw_map() +{ + const int width = m_map.get_width(); + const int height = m_map.get_height(); + const float span_x = 50; + const float span_y = 50; + float center_x = (span_x * width) / 2.0; + glPushAttrib(GL_POLYGON_BIT); + glEnable(GL_POLYGON_OFFSET_LINE); + glPushMatrix(); + glTranslatef(-center_x, 0, -100); + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + glPushMatrix(); + glTranslatef(span_x * x, span_y * y, 0); + glBegin(GL_QUADS); + glColor3f(0.4, 0.4, 0.4); + glVertex2f(span_x, span_y); + glVertex2f(0, span_y); + glVertex2f(0, 0); + glVertex2f(span_x, 0); + glEnd(); + glBegin(GL_LINE_LOOP); + glColor3f(1, 1, 1); + glVertex2f(span_x, span_y); + glVertex2f(0, span_y); + glVertex2f(0, 0); + glVertex2f(span_x, 0); + glEnd(); + glPopMatrix(); + } + } + glPopMatrix(); + glPopAttrib(); } diff --git a/src/client/Client.h b/src/client/Client.h index 0d09eb9..c4ab9d7 100755 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -4,6 +4,7 @@ #include #include "refptr.h" +#include "Map.h" class Client { @@ -13,8 +14,10 @@ class Client protected: void initgl(); void resize_window(int width, int height); + void draw_map(); refptr m_window; sf::Clock m_clock; + Map m_map; }; #endif diff --git a/src/common/Map.cc b/src/common/Map.cc new file mode 100755 index 0000000..7ed9973 --- /dev/null +++ b/src/common/Map.cc @@ -0,0 +1,16 @@ + +#include "Map.h" + +using namespace std; + +Map::Map(int width, int height) +{ + m_width = width; + m_height = height; + m_grid = vector< vector< bool > >(height, vector(width, true)); +} + +bool Map::tile_present(int x, int y) +{ + return m_grid[y][x]; +} diff --git a/src/common/Map.h b/src/common/Map.h new file mode 100755 index 0000000..d9a2327 --- /dev/null +++ b/src/common/Map.h @@ -0,0 +1,20 @@ + +#ifndef MAP_H +#define MAP_H + +#include + +class Map +{ + public: + Map(int width=50, int height=50); + bool tile_present(int x, int y); + int get_width() { return m_width; } + int get_height() { return m_height; } + protected: + int m_width; + int m_height; + std::vector< std::vector< bool > > m_grid; +}; + +#endif