1
1
Fork 0
Browse Source

initial player setup

thread-physics
alistair 5 years ago
parent
commit
06674d73d6
  1. 2
      Makefile
  2. 41
      draw.c
  3. 8
      draw.h
  4. BIN
      draw.o
  5. 87
      game.c
  6. 27
      game.h
  7. BIN
      game.o
  8. BIN
      main
  9. 29
      main.c
  10. BIN
      main.o

2
Makefile

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
CXX = gcc
SDL_INCLUDE = -lSDL2 -lSDL2_image
SDL_INCLUDE = -lSDL2 -lm -lSDL2_image
CXXFLAGS = -Wall $(SDL_INCLUDE)
EXE = main

41
draw.c

@ -3,7 +3,6 @@ @@ -3,7 +3,6 @@
/* generic rendering functions */
int MAX_ONSCREEN_OBJECTS = 99;
drawqueue_item objects_onscreen[100];
int num_onscreen = 0;
void render_texture_at(struct SDL_Renderer * ren, struct SDL_Texture * texture,int x, int y) {
@ -34,30 +33,8 @@ SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) { @@ -34,30 +33,8 @@ SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) {
/* Game Specific rendering functions */
void add_to_view(draw_type kind, void * object) {
if (num_onscreen == MAX_ONSCREEN_OBJECTS) {
logwrite(ERROR, "Maximum number of items on screen");
return;
}
drawqueue_item queue_item;
queue_item.kind = kind;
num_onscreen += 1;
objects_onscreen[num_onscreen -1] = queue_item;
}
void remove_from_view(int item, draw_type kind, void * object) {
/*
*objects_onscreen[item] = objects_onscreen[num_onscreen -1];
*num_onscreen -= 1;
*
*/
}
void draw_player(SDL_Renderer * ren, int x, int y) {
logwrite(DEBUG, "Drawing player");
logwrite(DEBUG, "Drawing player\n");
/* draw the player as a coloured rect */
SDL_Rect player_rect;
@ -77,16 +54,14 @@ void draw_player(SDL_Renderer * ren, int x, int y) { @@ -77,16 +54,14 @@ void draw_player(SDL_Renderer * ren, int x, int y) {
}
void redraw_buffer(SDL_Renderer * ren) {
for (int i = 0; i<num_onscreen; i++) {
drawqueue_item queue_item = objects_onscreen[i];
switch (queue_item.kind) {
case PLAYER: ;
player_st * object;
object = queue_item.player;
(*object).draw(ren, (*object).physics.x_pos, (*object).physics.y_pos);
for (int i=0; i<things_in_world; i++) {
world_thing thing;
thing = world[i];
switch (thing.kind) {
case PLAYER:
draw_player(ren, (*thing.player).physics.x_pos, (*thing.player).physics.y_pos);
break;
}
}
}

8
draw.h

@ -12,14 +12,6 @@ typedef enum { @@ -12,14 +12,6 @@ typedef enum {
PLAYER
} draw_type;
typedef struct {
draw_type kind;
union {
void * object;
player_st * player;
};
} drawqueue_item;
void render_texture_at(struct SDL_Renderer * ren, struct SDL_Texture * texture,int x, int y) ;
/* draw a texture at x.y */

BIN
draw.o

Binary file not shown.

87
game.c

@ -32,24 +32,22 @@ player_st get_player(int x, int y) { @@ -32,24 +32,22 @@ player_st get_player(int x, int y) {
player.physics.x_vel = 0;
player.physics.y_vel = 0;
/* Drawing */
void (*draw_function)(SDL_Renderer * ren, int x, int y);
draw_function = &draw_player;
player.draw = draw_function;
player.max_walking_speed = 5;
return (player);
}
void accel_thing(physics_thing * thing, float x, float y) {
void accel_thing(physics_thing * thing, long x, long y) {
/* takes acceleration in m/s2 and converts to m/ms adding
* it to physics_thing
*/
long x_adj = (long ) x / 1000.0;
long y_adj = (long ) y / 1000.0;
long x_adj = x / 1000.0;
long y_adj = y / 1000.0;
(*thing).y_acc += (float)y_adj;
(*thing).x_acc += (float)x_adj;
logwrite(DEBUG, "Accel thing\n");
}
void advance_thing(physics_thing * thing) {
@ -63,13 +61,74 @@ void advance_thing(physics_thing * thing) { @@ -63,13 +61,74 @@ void advance_thing(physics_thing * thing) {
(*thing).x_vel = (*thing).x_acc * (float)time_delta;
(*thing).y_vel = (*thing).y_acc * (float)time_delta;
(*thing).x_pos = (*thing).x_acc * 1/2 * (float)time_delta;
(*thing).y_pos = (*thing).y_acc * 1/2 * (float)time_delta;
(*thing).x_pos += (int)((*thing).x_acc * 1/2 * (float)time_delta * (float)time_delta);
(*thing).y_pos += (int)((*thing).y_acc * 1/2 * (float)time_delta * (float)time_delta);
printf("%f, %f \n", (*thing).x_acc, (*thing).y_acc );
}
void add_to_world(world_thing thing) {
world[things_in_world] = thing;
things_in_world += 1;
logwrite(INFO, "Added object to world");
}
void startgame(SDL_Renderer * ren) {
player_st player;
player = get_player(100,100);
// draw_player(ren, player);
add_to_view(PLAYER, (void *)&player);
things_in_world = 0;
player = get_player(100,300);
world_thing player_world;
player_world.kind = PLAYER_W;
player_world.player = &player;
add_to_world(player_world);
}
float get_abs(x,y) {
return (sqrt(x*x + y*y));
}
void walk(physics_thing * thing, int max_walking_speed, int x, int y) {
if (get_abs(x, y) < max_walking_speed) {
//accel_thing(thing, 0.001, 0.001 );
(*thing).x_acc = 0.00001;
(*thing).y_acc = 0;
}
}
void walk_player(int x, int y) {
walk(&player.physics, player.max_walking_speed, x, y);
}
void process_input(SDL_Keysym key) {
switch (key.sym) {
case SDLK_w:
walk_player(0, 1);
break;
case SDLK_a:
walk_player(-1, 0);
break;
case SDLK_s:
walk_player(0, -1);
break;
case SDLK_d:
walk_player(1, 0);
break;
}
}
void step(int interval) {
advance_thing(&(player.physics));
// calc time interval
//
// advance game physics
logwrite(DEBUG, "Update Physics \n");
}

27
game.h

@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
#include <SDL2/SDL_image.h>
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#ifndef _DEFGAME
@ -23,18 +24,36 @@ typedef struct { @@ -23,18 +24,36 @@ typedef struct {
} physics_thing;
typedef void (*drawplayer_func)(SDL_Renderer * ren, int x, int y);
typedef struct {
bool has_physics;
physics_thing physics;
drawplayer_func draw;
int max_walking_speed;
} player_st;
enum world_thing_kind {
PLAYER_W
};
typedef struct {
enum world_thing_kind kind;
union {
player_st * player;
};
} world_thing;
/* array of all the things in the world and their kinds */
world_thing world[100];
int things_in_world;
void startgame(SDL_Renderer * ren) ;
void process_input(SDL_Keysym key);
void step(int interval);
player_st player;
#endif

BIN
game.o

Binary file not shown.

BIN
main

Binary file not shown.

29
main.c

@ -26,12 +26,6 @@ struct SDL_Window* make_window(void) { @@ -26,12 +26,6 @@ struct SDL_Window* make_window(void) {
screen_width, screen_height, SDL_WINDOW_FULLSCREEN_DESKTOP);
}
void step(void) {
// calc time interval
//
// advance game physics
return;
}
void redraw(struct SDL_Renderer * ren) {
// check time
@ -41,9 +35,6 @@ void redraw(struct SDL_Renderer * ren) { @@ -41,9 +35,6 @@ void redraw(struct SDL_Renderer * ren) {
SDL_RenderClear(ren);
}
void refresh(struct SDL_Renderer * ren) {
}
void draw_pictures(struct SDL_Renderer * ren) {
logwrite(INFO, "Draw pictures\n");
// display an initial splash screen
@ -67,7 +58,7 @@ void draw_pictures(struct SDL_Renderer * ren) { @@ -67,7 +58,7 @@ void draw_pictures(struct SDL_Renderer * ren) {
int main () {
LOGLEVEL = DEBUG;
STDOUTLEVEL = SILENT;
STDOUTLEVEL = DEBUG;
logwrite(INFO, "Starting\n");
SDL_Window * win = make_window();
@ -86,10 +77,22 @@ int main () { @@ -86,10 +77,22 @@ int main () {
draw_pictures(ren);
bool splashscreen = true;
uint32_t last_advance_time;
uint32_t current_time;
while (!close) {
SDL_Event event;
/* Update Physics */
current_time = SDL_GetTicks();
if ((current_time - last_advance_time) > 100) {
step(100);
last_advance_time = current_time;
}
/* Redraw Screen */
redraw(ren);
/* Process events */
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
@ -101,6 +104,9 @@ int main () { @@ -101,6 +104,9 @@ int main () {
logwrite(DEBUG, "Starting Game");
startgame(ren);
}
SDL_Keysym key;
key = event.key.keysym;
process_input(key);
// send key to game engine
break;
@ -112,7 +118,6 @@ int main () { @@ -112,7 +118,6 @@ int main () {
}
}
printf("Cleanup queue length: %d\n", cleanup_queue_length);
empty_cleanup_queue();
printf("Cleanup queue length: %d\n", cleanup_queue_length);
logwrite(DEBUG, "Emptied cleanup queue\n");

BIN
main.o

Binary file not shown.
Loading…
Cancel
Save