alistair
5 years ago
14 changed files with 251 additions and 160 deletions
@ -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); |
||||
|
||||
|
||||
} |
@ -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 |
Binary file not shown.
@ -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 |
Binary file not shown.
@ -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); |
||||
} |
||||
} |
@ -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 |
||||
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue