1
1
Fork 0
Browse Source

time counter

breaks windows build
emscriptem
alistair 4 years ago
parent
commit
8c79376045
  1. 2
      Makefile
  2. BIN
      TerminusTTF.ttf
  3. 37
      src/draw.c
  4. 1
      src/draw.h
  5. 24
      src/game.c
  6. 1
      src/game.h
  7. 1
      src/main.c
  8. 3
      src/types.h

2
Makefile

@ -12,7 +12,7 @@ ifeq ($(target),windows)
CCFLAGS=$(SDL_INCLUDE) -O3 CCFLAGS=$(SDL_INCLUDE) -O3
else else
CC=gcc CC=gcc
SDL_INCLUDE= -lSDL2 -lm SDL_INCLUDE= -lSDL2 -lm -lSDL2_ttf
CCFLAGS=$(SDL_INCLUDE) -O3 CCFLAGS=$(SDL_INCLUDE) -O3
endif endif

BIN
TerminusTTF.ttf

Binary file not shown.

37
src/draw.c

@ -1,4 +1,5 @@
#include "draw.h" #include "draw.h"
#include "game.h"
Vect viewport_pos; Vect viewport_pos;
int width, height; int width, height;
@ -376,6 +377,39 @@ void draw_environment(SDL_Renderer * ren, struct environment *scene) {
} }
void draw_level_time(SDL_Renderer *ren, struct environment * e) {
static TTF_Font* font = NULL;
if (!font) {
font = TTF_OpenFont("TerminusTTF.ttf", 18);
const char *err = SDL_GetError();
if (err) {
printf("%s\n", err);
}
}
int margin = height / 10;
SDL_Rect position = {.x = margin, .y = height - margin};
struct colour colour = e->colours.fg1;
int minutes = level_time / 60000;
float seconds = level_time * 0.001;
char time_string[250];
snprintf(time_string, 250, "%d:%.4f", minutes, seconds);
SDL_Colour sdl_col = {colour.r, colour.g, colour.b, 255};
SDL_SetRenderDrawColor(ren, colour.r, colour.g, colour.b, 255);
SDL_Surface *surf = TTF_RenderText_Solid(font, time_string, sdl_col);
SDL_Texture *texture = SDL_CreateTextureFromSurface(ren, surf);
SDL_QueryTexture(texture, NULL, NULL, &position.w, &position.h);
SDL_RenderCopy(ren, texture, NULL, &position);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surf);
}
void redraw_buffer(SDL_Renderer * ren) { void redraw_buffer(SDL_Renderer * ren) {
static int mousex = 0; static int mousex = 0;
static int mousey = 0; static int mousey = 0;
@ -392,13 +426,12 @@ void redraw_buffer(SDL_Renderer * ren) {
/*}*/ /*}*/
/*}*/ /*}*/
int col = 0;
//SDL_GetMouseState(&newmousex, &newmousey); //SDL_GetMouseState(&newmousex, &newmousey);
update_viewport(player.physics->position.x, player.physics->position.y); update_viewport(player.physics->position.x, player.physics->position.y);
struct environment scene = get_scene_at(viewport_pos, level); struct environment scene = get_scene_at(viewport_pos, level);
draw_environment(ren, &scene); draw_environment(ren, &scene);
draw_level_time(ren, &scene);
for (int i=0; i < world.items.size; i++) { for (int i=0; i < world.items.size; i++) {
world_thing thing; world_thing thing;

1
src/draw.h

@ -2,6 +2,7 @@
#define _DEFDRAW #define _DEFDRAW
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "garbo.h" #include "garbo.h"
#include "c-colours/colours.h" #include "c-colours/colours.h"

24
src/game.c

@ -1,4 +1,5 @@
#include "game.h" #include "game.h"
#include <SDL2/SDL_ttf.h>
#define FLOOR_THICKNESS 200 #define FLOOR_THICKNESS 200
#define MAX_ROPE_GRAB_LEN 80000 #define MAX_ROPE_GRAB_LEN 80000
@ -11,6 +12,7 @@ player_st *glob_player;
SDL_Renderer *debug_ren; SDL_Renderer *debug_ren;
int level; int level;
long level_time;
/* array of all the things in the world and their kinds */ /* array of all the things in the world and their kinds */
//world_thing *world; //world_thing *world;
@ -47,6 +49,18 @@ void default_update_collision_poly(Body *body) {
} }
} }
void level_timer_update(bool reset) {
static int start_point = 0;
long now = SDL_GetTicks();
if (reset) {
level_time = 0;
start_point = now;
} else {
level_time = now - start_point;
}
}
// collision poly size must be 2x shape_size + 1 // collision poly size must be 2x shape_size + 1
void cast_update_collision_poly(Body *body) { void cast_update_collision_poly(Body *body) {
for (int i=0; i < body->collision_shape_size; i++) { for (int i=0; i < body->collision_shape_size; i++) {
@ -762,13 +776,14 @@ void ratcheted_winch_motor_update(Motor* motor) {
motor->stop = true; motor->stop = true;
return; return;
} }
winch_motor_update(motor);
Vect st; Vect st;
st.x = body->position.x - body->strings[0].end_point.x; st.x = body->position.x - body->strings[0].end_point.x;
st.y = body->position.y - body->strings[0].end_point.y; st.y = body->position.y - body->strings[0].end_point.y;
body->strings[0].max_length = vect_mag(st); if (body->strings[0].max_length > vect_mag(st))
body->strings[0].max_length = vect_mag(st);
winch_motor_update(motor);
} }
@ -1290,6 +1305,7 @@ void get_projectile(Projectile** proj) {
void advance_things(void) { void advance_things(void) {
int numcols; int numcols;
Vect translation; Vect translation;
level_timer_update(false);
for (int i = 0; i < world.items.size; i++) { for (int i = 0; i < world.items.size; i++) {
switch (world.get(i).kind) { switch (world.get(i).kind) {
case PLAYER_W : case PLAYER_W :
@ -1408,7 +1424,7 @@ void startgame(SDL_Renderer * ren) {
debug_ren = ren; debug_ren = ren;
level = 0; level = 113;
world = create_world(); world = create_world();
@ -1431,6 +1447,7 @@ void startgame(SDL_Renderer * ren) {
viewport_pos.x = 700; viewport_pos.x = 700;
viewport_pos.y = 0; viewport_pos.y = 0;
level_timer_update(true);
//get_room(); //get_room();
} }
@ -1505,6 +1522,7 @@ void step(int interval) {
if (player.physics->position.x > world.uniques_index[ROOM_W]->room if (player.physics->position.x > world.uniques_index[ROOM_W]->room
->floor.items[world.uniques_index[ROOM_W]->room->floor.numItems - 1]->position.x) { ->floor.items[world.uniques_index[ROOM_W]->room->floor.numItems - 1]->position.x) {
next_level(); next_level();
level_timer_update(true);
} }
advance_things(); advance_things();

1
src/game.h

@ -27,5 +27,6 @@ extern void process_keyup(SDL_Keysym key);
extern void step(int interval); extern void step(int interval);
extern player_st player; extern player_st player;
extern int level; extern int level;
extern long level_time;
#endif #endif

1
src/main.c

@ -65,6 +65,7 @@ int game(void) {
SDL_DestroyWindow(win); SDL_DestroyWindow(win);
SDL_Quit(); SDL_Quit();
} }
TTF_Init();
int close = 0; int close = 0;

3
src/types.h

@ -218,7 +218,8 @@ typedef struct world GlobWorld;
// environment // environment
enum { enum {
E_ROOM_WIDTH = 100000, // E_ROOM_WIDTH = 100000,
E_ROOM_WIDTH = 10000,
E_ROOM_RES = 500, E_ROOM_RES = 500,
E_ROOM_TILES = E_ROOM_WIDTH / E_ROOM_RES, E_ROOM_TILES = E_ROOM_WIDTH / E_ROOM_RES,
}; };

Loading…
Cancel
Save