diff --git a/Makefile b/Makefile index 69165c7..acb1be7 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ ifeq ($(target),windows) CCFLAGS=$(SDL_INCLUDE) -O3 else CC=gcc - SDL_INCLUDE= -lSDL2 -lm + SDL_INCLUDE= -lSDL2 -lm -lSDL2_ttf CCFLAGS=$(SDL_INCLUDE) -O3 endif diff --git a/TerminusTTF.ttf b/TerminusTTF.ttf new file mode 100644 index 0000000..d302264 Binary files /dev/null and b/TerminusTTF.ttf differ diff --git a/src/draw.c b/src/draw.c index 018a887..b91a896 100644 --- a/src/draw.c +++ b/src/draw.c @@ -1,4 +1,5 @@ #include "draw.h" +#include "game.h" Vect viewport_pos; 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) { static int mousex = 0; static int mousey = 0; @@ -392,13 +426,12 @@ void redraw_buffer(SDL_Renderer * ren) { /*}*/ /*}*/ - int col = 0; - //SDL_GetMouseState(&newmousex, &newmousey); update_viewport(player.physics->position.x, player.physics->position.y); struct environment scene = get_scene_at(viewport_pos, level); draw_environment(ren, &scene); + draw_level_time(ren, &scene); for (int i=0; i < world.items.size; i++) { world_thing thing; diff --git a/src/draw.h b/src/draw.h index 922f32a..2600311 100644 --- a/src/draw.h +++ b/src/draw.h @@ -2,6 +2,7 @@ #define _DEFDRAW #include +#include #include "garbo.h" #include "c-colours/colours.h" diff --git a/src/game.c b/src/game.c index 1525406..be636ee 100644 --- a/src/game.c +++ b/src/game.c @@ -1,4 +1,5 @@ #include "game.h" +#include #define FLOOR_THICKNESS 200 #define MAX_ROPE_GRAB_LEN 80000 @@ -11,6 +12,7 @@ player_st *glob_player; SDL_Renderer *debug_ren; int level; +long level_time; /* array of all the things in the world and their kinds */ //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 void cast_update_collision_poly(Body *body) { for (int i=0; i < body->collision_shape_size; i++) { @@ -762,13 +776,14 @@ void ratcheted_winch_motor_update(Motor* motor) { motor->stop = true; return; } - winch_motor_update(motor); Vect st; st.x = body->position.x - body->strings[0].end_point.x; 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) { int numcols; Vect translation; + level_timer_update(false); for (int i = 0; i < world.items.size; i++) { switch (world.get(i).kind) { case PLAYER_W : @@ -1408,7 +1424,7 @@ void startgame(SDL_Renderer * ren) { debug_ren = ren; - level = 0; + level = 113; world = create_world(); @@ -1431,6 +1447,7 @@ void startgame(SDL_Renderer * ren) { viewport_pos.x = 700; viewport_pos.y = 0; + level_timer_update(true); //get_room(); } @@ -1505,6 +1522,7 @@ void step(int interval) { if (player.physics->position.x > world.uniques_index[ROOM_W]->room ->floor.items[world.uniques_index[ROOM_W]->room->floor.numItems - 1]->position.x) { next_level(); + level_timer_update(true); } advance_things(); diff --git a/src/game.h b/src/game.h index d247daa..e3a4359 100644 --- a/src/game.h +++ b/src/game.h @@ -27,5 +27,6 @@ extern void process_keyup(SDL_Keysym key); extern void step(int interval); extern player_st player; extern int level; +extern long level_time; #endif diff --git a/src/main.c b/src/main.c index 700895a..7d412a9 100644 --- a/src/main.c +++ b/src/main.c @@ -65,6 +65,7 @@ int game(void) { SDL_DestroyWindow(win); SDL_Quit(); } + TTF_Init(); int close = 0; diff --git a/src/types.h b/src/types.h index e40640b..4566a42 100644 --- a/src/types.h +++ b/src/types.h @@ -218,7 +218,8 @@ typedef struct world GlobWorld; // environment enum { - E_ROOM_WIDTH = 100000, +// E_ROOM_WIDTH = 100000, + E_ROOM_WIDTH = 10000, E_ROOM_RES = 500, E_ROOM_TILES = E_ROOM_WIDTH / E_ROOM_RES, };