add a simple Map class and draw it from the Client

This commit is contained in:
Josh Holtrop 2012-08-20 20:13:41 -04:00
parent 3eff7d6097
commit 2c7e454ca8
5 changed files with 83 additions and 17 deletions

View File

@ -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)

View File

@ -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();
}

View File

@ -4,6 +4,7 @@
#include <SFML/Window.hpp>
#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<sf::Window> m_window;
sf::Clock m_clock;
Map m_map;
};
#endif

16
src/common/Map.cc Executable file
View File

@ -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<bool>(width, true));
}
bool Map::tile_present(int x, int y)
{
return m_grid[y][x];
}

20
src/common/Map.h Executable file
View File

@ -0,0 +1,20 @@
#ifndef MAP_H
#define MAP_H
#include <vector>
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