Created
January 31, 2014 15:46
-
-
Save shargoj/8734581 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <OpenGL/gl.h> | |
#include <OpenGL/glu.h> | |
#include <GLFW/glfw3.h> | |
#include <stdexcept> | |
#include "renderer.h" | |
#include "game_state.h" | |
#include "pos.h" | |
extern GLFWwindow *window; | |
namespace CH { | |
void set_gl_color(color c) { | |
switch (c) { | |
case RED: | |
glColor4f(1, 0, 0, 1); | |
break; | |
case YELLOW: | |
glColor4f(1, 1, 0, 1); | |
break; | |
case GREEN: | |
glColor4f(0, 1, 0, 1); | |
break; | |
case BLUE: | |
glColor4f(0, 0, 1, 1); | |
break; | |
case WALL: | |
glColor4f(0, 0, 0, 1); | |
break; | |
} | |
} | |
void draw_game_object(const game_object &go) { | |
set_gl_color(go.get_color()); | |
glPushMatrix(); | |
glTranslatef(go.get_position().x, go.get_position().y, 0.f); | |
glBegin(GL_QUADS); | |
glVertex3f(0.0f, 0.0f, -1.0f); | |
glVertex3f(0.0f, 1.0f, -1.0f); | |
glVertex3f(1.0f, 1.0f, -1.0f); | |
glVertex3f(1.0f, 0.0f, -1.0f); | |
glEnd(); | |
glPopMatrix(); | |
} | |
void renderer::render(const game_state gs) { | |
float w = static_cast<float>(gs.width), | |
h = static_cast<float>(gs.height); | |
float ratio = 1.0f / (480.0f / 640.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glOrtho(-ratio, ratio, -1, 1, 0.01, 4096.0); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
// Set up camera | |
glScalef(1.f, -1.f, 1.f); | |
glScalef(1.8f/w, 1.8f/h, 1.f); | |
glTranslatef(-w/2.0f, -h/2.0f, 0.0f); | |
glTranslatef(0.0f, 0.0f, -1.0f); | |
// Draw the walls | |
set_gl_color(WALL); | |
glBegin(GL_LINE_LOOP); | |
glVertex3f(0.0f, 0.0f, 0.0f); | |
glVertex3f(0.0f, h, 0.0f); | |
glVertex3f(w, h, 0.0f); | |
glVertex3f(w, 0.0f, 0.0f); | |
glEnd(); | |
// Draw the game objects | |
for (auto& pos_to_tail: gs.tails) { | |
draw_game_object(pos_to_tail.second); | |
} | |
for (auto& player: gs.players) { | |
draw_game_object(player); | |
} | |
glfwSwapBuffers(window); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment