1
1
Fork 0
Browse Source

more modularisation

thread-physics
alistair 5 years ago
parent
commit
c5b146506f
  1. 14
      Makefile
  2. 48
      draw.c
  3. 24
      draw.h
  4. BIN
      draw.o
  5. 21
      game.c
  6. 31
      game.h
  7. BIN
      game.o
  8. 62
      garbo.c
  9. 44
      garbo.h
  10. BIN
      garbo.o
  11. 6
      logger.h
  12. BIN
      main
  13. 161
      main.c
  14. BIN
      main.o

14
Makefile

@ -8,18 +8,24 @@ run: all @@ -8,18 +8,24 @@ run: all
all: $(EXE)
$(EXE): main.o logger.o game.o
$(CXX) $(CXXFLAGS) -o $(EXE) main.o game.o logger.o
$(EXE): main.o logger.o game.o garbo.o draw.o
$(CXX) $(CXXFLAGS) -o $(EXE) main.o game.o logger.o draw.o garbo.o
main.o: main.c logger.h game.h
main.o: main.c logger.h game.h garbo.h draw.h
$(CXX) $(CXXFLAGS) -c main.c
logger.o: logger.c logger.h
$(CXX) $(CXXFLAGS) -c logger.c
game.o: game.c game.h
game.o: game.c game.h draw.h
$(CXX) $(CXXFLAGS) -c game.c
garbo.o: garbo.c garbo.h
$(CXX) $(CXXFLAGS) -c garbo.c
draw.o: draw.c draw.h
$(CXX) $(CXXFLAGS) -c draw.c
clean:
rm *.o && rm $(EXE)

48
draw.c

@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
#include "draw.h"
/* generic rendering functions */
void render_texture_at(struct SDL_Renderer * ren, struct SDL_Texture * texture,int x, int y) {
/* draw a texture at x.y */
SDL_Rect destination;
destination.x = x;
destination.y = y;
SDL_QueryTexture(texture, NULL, NULL, &destination.w, &destination.h);
SDL_RenderCopy(ren, texture, NULL, &destination);
}
SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) {
/* Load an image into a texture */
SDL_Surface * surface = IMG_Load(fname) ;
if(!surface) {
printf("IMG_Load: %s\n", IMG_GetError());
// handle error
}
SDL_Texture * png_image = SDL_CreateTextureFromSurface(ren, surface);
cleanup(surface, SURFACE);
return (png_image);
}
/* Game Specific rendering functions */
void draw_player(SDL_Renderer * ren, player_st player) {
/* draw the player as a coloured rect */
SDL_Rect player_rect;
player_rect.x = player.physics.x_pos;
player_rect.y = player.physics.y_pos;
player_rect.w = 10;
player_rect.h = 10;
SDL_SetRenderDrawColor(ren, 0,0,0, 255);
SDL_RenderDrawRect(ren, &player_rect);
SDL_SetRenderDrawColor(ren, 120, 85, 188, 255);
SDL_RenderFillRect(ren, &player_rect);
}

24
draw.h

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "garbo.h"
#include "game.h"
#ifndef _DEFDRAW
#define _DEFDRAW
typedef struct {
} gameview_object;
void render_texture_at(struct SDL_Renderer * ren, struct SDL_Texture * texture,int x, int y) ;
/* draw a texture at x.y */
SDL_Texture * load_image(SDL_Renderer * ren, char fname[]);
/* Load an image into a texture */
void draw_player(SDL_Renderer * ren, player_st player);
/* draw the player as a coloured rect */
#endif

BIN
draw.o

Binary file not shown.

21
game.c

@ -1,20 +1,6 @@ @@ -1,20 +1,6 @@
#include "game.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>
#include <stdint.h>
typedef struct {
int x_pos;
int y_pos;
float x_vel;
float y_vel;
float x_acc;
float y_acc;
#include "game.h"
} physics_thing;
/*
*typedef enum {
@ -32,11 +18,6 @@ typedef struct { @@ -32,11 +18,6 @@ typedef struct {
*/
typedef struct {
bool has_physics;
physics_thing physics;
} player_st;
player_st get_player(int x, int y) {
/* creates player at given postion and zeroes physics */

31
game.h

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>
#include <stdint.h>
#include "garbo.h"
#ifndef _DEFGAME
#define _DEFGAME
typedef struct {
int x_pos;
int y_pos;
float x_vel;
float y_vel;
float x_acc;
float y_acc;
} physics_thing;
typedef struct {
bool has_physics;
physics_thing physics;
} player_st;
#endif

BIN
game.o

Binary file not shown.

62
garbo.c

@ -0,0 +1,62 @@ @@ -0,0 +1,62 @@
#include "garbo.h"
void queue_for_cleanup(void * SDL_thing, sdl_types kind) {
// add item to queue for later cleanup
// TODO: support multiple cleanup queues
sdl_abstract obj;
obj.kind = kind;
switch (kind) {
case TEXTURE:
obj.thing.texture = SDL_thing;
break;
case WINDOW:
obj.thing.window = SDL_thing;
break;
case RENDERER:
obj.thing.renderer = SDL_thing;
break;
case SURFACE:
obj.thing.surface = SDL_thing;
break;
}
cleanup_queue_length++;
cleanup_queue[cleanup_queue_length] = obj;
}
void cleanup(void * thing, sdl_types thing_type) {
// Destroy an SDL object immediately
if (!thing) {
logwrite(DEBUG, "Null passed to cleanup\n");
return;
}
switch(thing_type) {
case SURFACE:
SDL_FreeSurface(thing);
logwrite(DEBUG, "Freed surface\n");
break;
case TEXTURE:
SDL_DestroyTexture(thing);
logwrite(DEBUG, "Destroyed texture\n");
break;
case RENDERER:
SDL_DestroyRenderer(thing);
logwrite(DEBUG, "Destroyed renderer\n");
break;
case WINDOW:
SDL_DestroyWindow(thing);
logwrite(DEBUG, "Destroyed window\n");
break;
case NO:
return;
}
}
void empty_cleanup_queue(void) {
// empty the queue of items scheduled for cleanup
for (; cleanup_queue_length > 0; cleanup_queue_length--) {
sdl_abstract thing = cleanup_queue[cleanup_queue_length];
cleanup(thing.thing.generic, thing.kind);
}
}

44
garbo.h

@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <string.h>
#include "logger.h"
#include <stdbool.h>
#ifndef _DEFGARBO
#define _DEFGARBO
/* Type definition for object cleanups */
typedef enum {
NO, SURFACE, TEXTURE, RENDERER, WINDOW
} sdl_types;
typedef union {
SDL_Window * window;
SDL_Renderer * renderer;
SDL_Surface * surface;
SDL_Texture * texture;
void * generic;
} thing_for_cleanup ;
typedef struct {
sdl_types kind;
thing_for_cleanup thing;
} sdl_abstract;
static sdl_abstract cleanup_queue[100];
static int cleanup_queue_length = 0;
/* Function prototypes */
void queue_for_cleanup(void * SDL_thing, sdl_types kind);
void cleanup(void * thing, sdl_types thing_type);
void empty_cleanup_queue(void);
#endif

BIN
garbo.o

Binary file not shown.

6
logger.h

@ -2,6 +2,10 @@ @@ -2,6 +2,10 @@
#include <string.h>
#include <stdio.h>
#ifndef _DEFLOGGER
#define _DEFLOGGER
typedef enum {
// high-low priority
// low-high verbosity
@ -15,3 +19,5 @@ extern loggerlevel STDOUTLEVEL; @@ -15,3 +19,5 @@ extern loggerlevel STDOUTLEVEL;
void logwrite(loggerlevel level, char message[]);
void set_loglevel(loggerlevel echo, loggerlevel write);
#endif

BIN
main

Binary file not shown.

161
main.c

@ -5,88 +5,16 @@ @@ -5,88 +5,16 @@
#include <SDL2/SDL_image.h>
#include <string.h>
#include <time.h>
#include "logger.h"
#include <stdbool.h>
#include "logger.h"
#include "game.h"
#include "draw.h"
#include "garbo.h"
const int screen_width = 800;
const int screen_height = 600;
/* Type definition for object cleanups */
typedef enum {
NO, SURFACE, TEXTURE, RENDERER, WINDOW
} sdl_types;
typedef union {
SDL_Window * window;
SDL_Renderer * renderer;
SDL_Surface * surface;
SDL_Texture * texture;
void * generic;
} thing_for_cleanup ;
typedef struct {
sdl_types kind;
thing_for_cleanup thing;
} sdl_abstract;
static sdl_abstract cleanup_queue[100];
static int cleanup_queue_length = 0;
void queue_for_cleanup(void * SDL_thing, sdl_types kind) {
sdl_abstract obj;
obj.kind = kind;
switch (kind) {
case TEXTURE:
obj.thing.texture = SDL_thing;
break;
case WINDOW:
obj.thing.window = SDL_thing;
break;
case RENDERER:
obj.thing.renderer = SDL_thing;
break;
case SURFACE:
obj.thing.surface = SDL_thing;
break;
}
cleanup_queue_length++;
cleanup_queue[cleanup_queue_length] = obj;
}
void cleanup(void * thing, sdl_types thing_type) {
if (!thing) {
logwrite(DEBUG, "Null passed to cleanup\n");
return;
}
switch(thing_type) {
case SURFACE:
SDL_FreeSurface(thing);
logwrite(DEBUG, "Freed surface\n");
break;
case TEXTURE:
SDL_DestroyTexture(thing);
logwrite(DEBUG, "Destroyed texture\n");
break;
case RENDERER:
SDL_DestroyRenderer(thing);
logwrite(DEBUG, "Destroyed renderer\n");
break;
case WINDOW:
SDL_DestroyWindow(thing);
logwrite(DEBUG, "Destroyed window\n");
break;
case NO:
return;
}
}
void empty_cleanup_queue(void) {
for (; cleanup_queue_length > 0; cleanup_queue_length--) {
sdl_abstract thing = cleanup_queue[cleanup_queue_length];
cleanup(thing.thing.generic, thing.kind);
}
}
struct SDL_Window* make_window(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
@ -95,7 +23,7 @@ struct SDL_Window* make_window(void) { @@ -95,7 +23,7 @@ struct SDL_Window* make_window(void) {
return SDL_CreateWindow("sdl_tester",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800, 600, SDL_WINDOW_FULLSCREEN_DESKTOP);
screen_width, screen_height, SDL_WINDOW_FULLSCREEN_DESKTOP);
}
void step(void) {
@ -105,42 +33,14 @@ void step(void) { @@ -105,42 +33,14 @@ void step(void) {
return;
}
void redraw(void) {
void redraw(struct SDL_Renderer * ren) {
// check time
// redraw if 1/60th of second passed
}
void render_texture_at(struct SDL_Renderer * ren, struct SDL_Texture * texture,int x, int y) {
SDL_Rect destination;
destination.x = x;
destination.y = y;
SDL_QueryTexture(texture, NULL, NULL, &destination.w, &destination.h);
SDL_RenderCopy(ren, texture, NULL, &destination);
// queue for cleanup
}
void queue_object_for_cleanup(void * thing, sdl_types thing_type) {
SDL_RenderPresent(ren);
SDL_RenderClear(ren);
}
void refresh(struct SDL_Renderer * ren) {
}
SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) {
SDL_Surface * surface = IMG_Load(fname) ;
if(!surface) {
printf("IMG_Load: %s\n", IMG_GetError());
// handle error
}
SDL_Texture * png_image = SDL_CreateTextureFromSurface(ren, surface);
cleanup(surface, SURFACE);
return (png_image);
}
void draw_pictures(struct SDL_Renderer * ren) {
@ -156,19 +56,11 @@ void draw_pictures(struct SDL_Renderer * ren) { @@ -156,19 +56,11 @@ void draw_pictures(struct SDL_Renderer * ren) {
*/
SDL_Texture * png_image = load_image(ren, "trans.PNG"); //SDL_CreateTextureFromSurface(ren, png_surface);
// remove the surface from memory as no longer needed
//cleanup(png_surface, SURFACE);
//cleanup(surface, SURFACE);
// check this actually does stuff
SDL_RenderClear(ren);
//render_texture_at(ren, texture, 100, 100);
//render_texture_at(ren, png_image, 100, 100);
SDL_RenderCopy(ren, png_image, NULL, NULL);
// update the actual screen
SDL_RenderPresent(ren);
}
int main () {
@ -178,7 +70,7 @@ int main () { @@ -178,7 +70,7 @@ int main () {
logwrite(INFO, "Starting\n");
SDL_Window * win = make_window();
SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
queue_for_cleanup(win, WINDOW);
queue_for_cleanup(ren, RENDERER);
@ -196,25 +88,22 @@ int main () { @@ -196,25 +88,22 @@ int main () {
while (!close) {
SDL_Event event;
redraw(ren);
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
close = 1;
break;
case SDL_KEYDOWN:
splashscreen = false;
// send key to game engine
break;
default:
if (splashscreen) {
draw_pictures(ren);
}
break;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
close = 1;
break;
case SDL_KEYDOWN:
SDL_RenderClear(ren);
SDL_RenderPresent(ren);
splashscreen = false;
// send key to game engine
break;
default:
if (splashscreen) {
draw_pictures(ren);
}
break;
}
}
}
printf("Cleanup queue length: %d\n", cleanup_queue_length);

BIN
main.o

Binary file not shown.
Loading…
Cancel
Save