1
1
Fork 0
Browse Source

first physcs work

thread-physics
alistair 5 years ago
parent
commit
5cbaf412bc
  1. 11
      Makefile
  2. 79
      game.c
  3. 0
      game.h
  4. BIN
      game.o
  5. 5
      logger.c
  6. 2
      logger.h
  7. BIN
      logger.h.gch
  8. BIN
      logger.o
  9. BIN
      main
  10. 40
      main.c
  11. BIN
      main.o

11
Makefile

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
CXX = gcc
SDL_INCLUDE = -lSDL2 -lSDL2_image
CXXFLAGS = $(SDL_INCLUDE)
CXXFLAGS = -Wall $(SDL_INCLUDE)
EXE = main
run: all
@ -8,15 +8,18 @@ run: all @@ -8,15 +8,18 @@ run: all
all: $(EXE)
$(EXE): main.o logger.o
$(CXX) $(CXXFLAGS) -o $(EXE) main.o logger.o
$(EXE): main.o logger.o game.o
$(CXX) $(CXXFLAGS) -o $(EXE) main.o game.o logger.o
main.o: main.c logger.h
main.o: main.c logger.h game.h
$(CXX) $(CXXFLAGS) -c main.c
logger.o: logger.c logger.h
$(CXX) $(CXXFLAGS) -c logger.c
game.o: game.c game.h
$(CXX) $(CXXFLAGS) -c game.c
clean:
rm *.o && rm $(EXE)

79
game.c

@ -0,0 +1,79 @@ @@ -0,0 +1,79 @@
#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;
} physics_thing;
/*
*typedef enum {
* sdl_shape;
* image;
*
*} render_type;
*/
/*
*typedef struct {
* char image_path[];
* // poly
*} sprite;
*/
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 */
player_st player;
player.physics.x_pos = x;
player.physics.y_pos = y;
player.physics.x_acc = 0;
player.physics.y_acc = 0;
player.physics.x_vel = 0;
player.physics.y_vel = 0;
return (player);
}
void accel_thing(physics_thing * thing, float x, float 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;
(*thing).y_acc += (float)y_adj;
(*thing).x_acc += (float)x_adj;
}
void advance_thing(physics_thing * thing) {
static uint32_t last_advance_time;
uint16_t time_delta = last_advance_time - SDL_GetTicks();
// milliseconds
last_advance_time = SDL_GetTicks();
(*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;
}

BIN
game.o

Binary file not shown.

5
logger.c

@ -6,6 +6,11 @@ @@ -6,6 +6,11 @@
loggerlevel LOGLEVEL = DEBUG;
loggerlevel STDOUTLEVEL = INFO;
void set_loglevel(loggerlevel echo, loggerlevel write) {
LOGLEVEL = write;
STDOUTLEVEL = echo;
}
int copy_str(char * out_string, char * string) {
/* A drop in replacement for strcpy() from <string.h>*/
int len = 0;

2
logger.h

@ -13,3 +13,5 @@ extern loggerlevel LOGLEVEL; @@ -13,3 +13,5 @@ extern loggerlevel LOGLEVEL;
extern loggerlevel STDOUTLEVEL;
void logwrite(loggerlevel level, char message[]);
void set_loglevel(loggerlevel echo, loggerlevel write);

BIN
logger.h.gch

Binary file not shown.

BIN
logger.o

Binary file not shown.

BIN
main

Binary file not shown.

40
main.c

@ -6,16 +6,11 @@ @@ -6,16 +6,11 @@
#include <string.h>
#include <time.h>
#include "logger.h"
#include <stdbool.h>
const int screen_width = 800;
const int screen_height = 600;
/* logging definition */
//LOGLEVEL = DEBUG;
//STDOUTLEVEL = DEBUG;
/* Type definition for object cleanups */
typedef enum {
NO, SURFACE, TEXTURE, RENDERER, WINDOW
@ -53,7 +48,6 @@ void queue_for_cleanup(void * SDL_thing, sdl_types kind) { @@ -53,7 +48,6 @@ void queue_for_cleanup(void * SDL_thing, sdl_types kind) {
case SURFACE:
obj.thing.surface = SDL_thing;
break;
}
cleanup_queue_length++;
@ -101,7 +95,7 @@ struct SDL_Window* make_window(void) { @@ -101,7 +95,7 @@ struct SDL_Window* make_window(void) {
return SDL_CreateWindow("sdl_tester",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800, 600, 0);
800, 600, SDL_WINDOW_FULLSCREEN_DESKTOP);
}
void step(void) {
@ -149,12 +143,10 @@ SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) { @@ -149,12 +143,10 @@ SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) {
return (png_image);
}
void draw_pictures(struct SDL_Renderer * ren) {
logwrite(INFO, "Draw pictures\n");
// display an initial splash screen
char png_file[] = "trans.PNG";
/*
*SDL_Surface * png_surface = IMG_Load(png_file) ;
*if(!png_surface) {
@ -163,7 +155,7 @@ void draw_pictures(struct SDL_Renderer * ren) { @@ -163,7 +155,7 @@ void draw_pictures(struct SDL_Renderer * ren) {
*}
*/
SDL_Texture * png_image = load_image(ren, png_file); //SDL_CreateTextureFromSurface(ren, png_surface);
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);
@ -177,12 +169,13 @@ void draw_pictures(struct SDL_Renderer * ren) { @@ -177,12 +169,13 @@ void draw_pictures(struct SDL_Renderer * ren) {
// update the actual screen
SDL_RenderPresent(ren);
}
int main () {
LOGLEVEL = DEBUG;
STDOUTLEVEL = DEBUG;
int main() {
logwrite(INFO, "Starting\n");
SDL_Window * win = make_window();
SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
@ -197,7 +190,9 @@ int main() { @@ -197,7 +190,9 @@ int main() {
}
int close = 0;
int first = 1;
draw_pictures(ren);
bool splashscreen = true;
while (!close) {
SDL_Event event;
@ -207,18 +202,21 @@ int main() { @@ -207,18 +202,21 @@ int main() {
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 (!first) {
step();
} else {
if (splashscreen) {
draw_pictures(ren);
first = 1;
}
break;
}
}
}
printf("Cleanup queue length: %d\n", cleanup_queue_length);
empty_cleanup_queue();
printf("Cleanup queue length: %d\n", cleanup_queue_length);

BIN
main.o

Binary file not shown.
Loading…
Cancel
Save