From 8398a09cf14d854ae0ef67af8c781b5f520dcfef Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 17 Feb 2013 23:11:33 -0500 Subject: [PATCH] start splitting game logic into Game class --- Makefile | 2 +- src/game.d | 181 ++++++++--------------------------------------------- src/main.d | 135 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+), 157 deletions(-) create mode 100644 src/main.d diff --git a/Makefile b/Makefile index 331399c..c5a7076 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ DC := gdc TARGET := game SOURCES := $(shell find src -iname '*.d') OBJECTS := $(patsubst %.d,%.o,$(SOURCES)) -DFLAGS := -I/usr/local/include/d +DFLAGS := -I/usr/local/include/d -Isrc LDFLAGS := -lDerelictSDL -lDerelictGL -lDerelictGLU -lDerelictUtil -ldl -lGL -lGLU all: $(TARGET) diff --git a/src/game.d b/src/game.d index 348b3f8..7a1615a 100644 --- a/src/game.d +++ b/src/game.d @@ -1,168 +1,37 @@ -import std.stdio; -import derelict.sdl.sdl; -import derelict.opengl.gl; -import derelict.opengl.glu; +static enum uint TICK_MS = 50; -enum int WIDTH = 800; -enum int HEIGHT = 600; -enum uint SDL_TICKS_PER_GAME_TICK = 1000u / 20u; - -class Player +enum { - public: - int x; - int y; - uint tick; + MOVE_LEFT, + MOVE_RIGHT, + MOVE_UP, + MOVE_DOWN +} + +struct Player +{ + int x; + int y; }; -class Inputs +struct MoveEvent { - public: - bool left; - bool right; - bool up; - bool down; + ubyte type; + bool stop; + uint tick; }; -void init() +class Game { - glClearColor (0.0, 0.0, 0.0, 0.0); - glShadeModel(GL_SMOOTH); - glViewport(0, 0, WIDTH, HEIGHT); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0, WIDTH, 0, HEIGHT, 1.0, -1.0); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); -} - -void display(Player p1, Player p2) -{ - glClear(GL_COLOR_BUFFER_BIT); - glPushMatrix(); - glTranslatef(p1.x, p1.y, 0); - glBegin(GL_QUADS); - glColor3f(1, 0, 0); - glVertex3f(10, 20, 0.0); - glVertex3f(-10, 20, 0.0); - glVertex3f(-10, -20, 0.0); - glVertex3f(10, -20, 0.0); - glEnd(); - glPopMatrix(); - glPushMatrix(); - glTranslatef(p2.x, p2.y, 0); - glBegin(GL_QUADS); - glColor3f(0, 0, 1); - glVertex3f(10, 20, 0.0); - glVertex3f(-10, 20, 0.0); - glVertex3f(-10, -20, 0.0); - glVertex3f(10, -20, 0.0); - glEnd(); - glPopMatrix(); - SDL_GL_SwapBuffers(); -} - -int main(char[][] args) -{ - DerelictSDL.load(); - DerelictGL.load(); - DerelictGLU.load(); - - Player p1 = new Player(); - Player p2 = new Player(); - p1.x = WIDTH / 2 - 100; - p1.y = HEIGHT / 2; - p2.x = WIDTH / 2 + 100; - p2.y = HEIGHT / 2; - int pcontrol = 0; - Player players[] = [p1, p2]; - - if (SDL_Init(SDL_INIT_EVERYTHING)) +public: + void change_player() { - writefln("Failed to initialize SDL!"); - return 1; } - - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - - SDL_Surface *screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL); - if (screen == null) + void handle_move_event(ref MoveEvent me) { - printf("Failed to set video mode!\n"); - SDL_Quit(); - return 2; } - - uint last_sdl_tick; - init(); - SDL_Event event; - Inputs current_inputs = new Inputs(); - bool exit = false; - for (;;) - { - if (SDL_GetTicks() >= (last_sdl_tick + SDL_TICKS_PER_GAME_TICK)) - { - players[pcontrol].tick++; - last_sdl_tick += SDL_TICKS_PER_GAME_TICK; - } - if (SDL_PollEvent(&event)) - { - switch (event.type) - { - case SDL_QUIT: - exit = true; - break; - case SDL_KEYDOWN: - switch (event.key.keysym.sym) - { - case SDLK_ESCAPE: - exit = true; - break; - case SDLK_a: - current_inputs.left = true; - break; - case SDLK_d: - current_inputs.right = true; - break; - case SDLK_w: - current_inputs.up = true; - break; - case SDLK_s: - current_inputs.down = true; - break; - default: - break; - } - break; - case SDL_KEYUP: - switch (event.key.keysym.sym) - { - case SDLK_a: - current_inputs.left = false; - break; - case SDLK_d: - current_inputs.right = false; - break; - case SDLK_w: - current_inputs.up = false; - break; - case SDLK_s: - current_inputs.down = false; - break; - default: - break; - } - break; - default: - break; - } - } - if (exit) - break; - display(p1, p2); - } - - SDL_Quit(); - - return 0; -} + Player[] get_players() { return m_players; } +protected: + uint m_tick; + Player[2] m_players; +}; diff --git a/src/main.d b/src/main.d new file mode 100644 index 0000000..20bc405 --- /dev/null +++ b/src/main.d @@ -0,0 +1,135 @@ +import std.stdio; +import derelict.sdl.sdl; +import derelict.opengl.gl; +import derelict.opengl.glu; + +import game; + +enum int WIDTH = 800; +enum int HEIGHT = 600; + +void init() +{ + glClearColor (0.0, 0.0, 0.0, 0.0); + glShadeModel(GL_SMOOTH); + glViewport(0, 0, WIDTH, HEIGHT); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, WIDTH, 0, HEIGHT, 1.0, -1.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +void display(Game g) +{ + Player[] players = g.get_players(); + glClear(GL_COLOR_BUFFER_BIT); + glPushMatrix(); + glTranslatef(players[0].x, players[0].y, 0); + glBegin(GL_QUADS); + glColor3f(1, 0, 0); + glVertex3f(10, 20, 0.0); + glVertex3f(-10, 20, 0.0); + glVertex3f(-10, -20, 0.0); + glVertex3f(10, -20, 0.0); + glEnd(); + glPopMatrix(); + glPushMatrix(); + glTranslatef(players[1].x, players[1].y, 0); + glBegin(GL_QUADS); + glColor3f(0, 0, 1); + glVertex3f(10, 20, 0.0); + glVertex3f(-10, 20, 0.0); + glVertex3f(-10, -20, 0.0); + glVertex3f(10, -20, 0.0); + glEnd(); + glPopMatrix(); + SDL_GL_SwapBuffers(); +} + +int main(char[][] args) +{ + DerelictSDL.load(); + DerelictGL.load(); + DerelictGLU.load(); + + if (SDL_Init(SDL_INIT_EVERYTHING)) + { + writefln("Failed to initialize SDL!"); + return 1; + } + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + SDL_Surface *screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL); + if (screen == null) + { + printf("Failed to set video mode!\n"); + SDL_Quit(); + return 2; + } + + init(); + SDL_Event event; + bool exit = false; + Game game = new Game(); + game.get_players()[0].x = WIDTH / 2 - 100; + game.get_players()[0].y = HEIGHT / 2; + game.get_players()[1].x = WIDTH / 2 + 100; + game.get_players()[1].y = HEIGHT / 2; + + for (;;) + { + if (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_QUIT: + exit = true; + break; + case SDL_KEYDOWN: + switch (event.key.keysym.sym) + { + case SDLK_ESCAPE: + exit = true; + break; + case SDLK_a: + break; + case SDLK_d: + break; + case SDLK_w: + break; + case SDLK_s: + break; + default: + break; + } + break; + case SDL_KEYUP: + switch (event.key.keysym.sym) + { + case SDLK_a: + break; + case SDLK_d: + break; + case SDLK_w: + break; + case SDLK_s: + break; + default: + break; + } + break; + default: + break; + } + } + if (exit) + break; + display(game); + } + + SDL_Quit(); + + return 0; +}