diff options
| -rwxr-xr-x | Makefile | 1 | ||||
| -rw-r--r-- | source/all.h | 39 | ||||
| -rw-r--r-- | source/game.c | 10 | ||||
| -rw-r--r-- | source/main.c | 50 | ||||
| -rw-r--r-- | source/render.c | 30 |
5 files changed, 112 insertions, 18 deletions
@@ -16,7 +16,6 @@ endif SOURCE.dir := source OBJECT.dir := object -HEADER := $(wildcard *.h) # yeah it's duplicative however that is unimportant SOURCE.orig := $(wildcard ${SOURCE.dir}/*.c) SOURCE.orig := $(SOURCE.orig:${SOURCE.dir}/%=%) SOURCE := $(addprefix ${SOURCE.dir}/, $(SOURCE.orig)) diff --git a/source/all.h b/source/all.h new file mode 100644 index 0000000..b8f13bd --- /dev/null +++ b/source/all.h @@ -0,0 +1,39 @@ +#ifndef EVERYTHING_ALWAYS_H_ +#define EVERYTHING_ALWAYS_H_ + +#include <stdio.h> +#include <stdint.h> +#include <math.h> +#include <raylib.h> + +#define TEXT_BUFFER_LIMIT (1<<12) +#define FRAME_LIMIT (1<<4) + +typedef struct { + Font font; + float frame_x[FRAME_LIMIT]; + float frame_y[FRAME_LIMIT]; + int horizontal, vertical; +} game_t; + +/* render.c */ + +/* Everything here assumes White On Black = OK. for now. */ +/* Things should be textured and have backgrounds which is a lot of extra params, + which are not important right now. */ + +/* I would prefer that things remain CENTERED as that makes preportional to WINDOW SIZE shit easier */ +/* The Window is resizable, by the way. */ + +void draw_square_grid (game_t * game, size_t frame, int size, Texture * texture, int * array, size_t length); +void draw_vertical_bargraph (game_t * game, size_t frame, int size, Color color, int * arr, size_t length); +void draw_centered_text (game_t * game, size_t frame, int font_size, Color color, char * format, ...); + +/* game.c */ + +void game_frame(game_t * game, size_t frame, float x, float y); +Vector2 game_frame_vector(game_t * game, size_t frame); + +/* ... */ + +#endif /* EVERYTHING_ALWAYS_H_ */ diff --git a/source/game.c b/source/game.c new file mode 100644 index 0000000..941b176 --- /dev/null +++ b/source/game.c @@ -0,0 +1,10 @@ +#include "all.h" + +void game_frame(game_t * game, size_t frame, float x, float y) { + game->frame_x[frame] = x; + game->frame_y[frame] = y; +} + +Vector2 game_frame_vector(game_t * game, size_t frame) { + return (Vector2) { game->frame_x[frame], game->frame_y[frame]}; +} diff --git a/source/main.c b/source/main.c index d1cb359..1cf3c55 100644 --- a/source/main.c +++ b/source/main.c @@ -1,45 +1,59 @@ -#include <stdio.h> - -#include <stdint.h> #include <time.h> #include <unistd.h> - -#include <raylib.h> #include <rlgl.h> +#include "all.h" + +/* I really like this in usage. */ + +static inline void frame_reset(game_t * game) { + game_frame(game, 0, game->horizontal/12, game->vertical/12); + game_frame(game, 1, game->horizontal/3, game->vertical - game->vertical/8); + game_frame(game, 2, game->horizontal - game->horizontal/4, game->vertical/12); +} + int main (int count, char ** arguments) { + (void)count; + game_t game[1] = {0}; + game->horizontal = 1920; + game->vertical = 1080; /* :config */ - int horizontal = 1920, vertical = 1080; - + frame_reset(game); { /* this idented (DENTED) style is autistic and dumb but I like it visually */ SetConfigFlags(FLAG_WINDOW_RESIZABLE); /* tell raylib to shut up */ /* SetTraceLogLevel(LOG_NONE); */ - InitWindow(horizontal, vertical, arguments[0]); + InitWindow(game->horizontal, game->vertical, arguments[0]); SetWindowState(FLAG_WINDOW_HIDDEN); InitAudioDevice(); SetWindowPosition(0, 0); } + game->font = GetFontDefault(); + /* :todo ping me I'll update this to a u/f seperated game loop */ { /* loop to end all loops */ - uint64_t frame = 0; + uint64_t fc = 0; /* :config */ float fps = 30; - double wait = wait = 1.0 / fps, Δ = wait; + double wait = wait = 1.0 / fps, delta = wait; struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); ClearWindowState(FLAG_WINDOW_HIDDEN); while (1) { - if (Δ > wait) { + if (delta > wait) { clock_gettime(CLOCK_MONOTONIC, &start); { /* update */ - ++frame; + ++fc; PollInputEvents(); + if (IsWindowResized()) { + game->horizontal = GetScreenWidth(), game->vertical = GetScreenHeight(); + frame_reset(game); + } /* physical keys */ switch (GetKeyPressed()) { - case KEY_Q: goto stop; + /* case KEY_Q: goto stop; */ case KEY_ESCAPE: goto stop; } /* routed keys */ @@ -48,15 +62,17 @@ int main (int count, char ** arguments) { /* draw */ BeginDrawing(); ClearBackground(BLACK); - DrawText(TextFormat("Delta (Δ): %d", Δ), 10, 10, 20, WHITE); + draw_square_grid(game, 0, 50, NULL, (int[]) {1,2,3, 4,5,6, 7,8,9}, 9); + draw_centered_text(game, 1, 20, WHITE, "Snails are now preparing!"); + draw_centered_text(game, 2, 20, WHITE, "Gambling here"); rlDrawRenderBatchActive(); SwapScreenBuffer(); } } clock_gettime(CLOCK_MONOTONIC, &end); - Δ = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9; - if (Δ < wait) { - double should = -(Δ - wait); + delta = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9; + if (delta < wait) { + double should = -(delta - wait); if (should > 0) { usleep(1e6 * should); } } } diff --git a/source/render.c b/source/render.c new file mode 100644 index 0000000..3271b99 --- /dev/null +++ b/source/render.c @@ -0,0 +1,30 @@ +#include "all.h" + +void draw_square_grid(game_t * game, size_t frame, int size, Texture * texture, int * array, size_t length) { + (void)texture; + float square_length = size; + size_t square = floor(sqrt(length)); + size_t j, i; + float x = game->frame_x[frame], y = game->frame_y[frame]; + for (i = 0; i < square; ++i) { + for (j = 0; j < square; ++j) { + DrawRectangleV((Vector2){x+i*square_length, y+j*square_length}, + (Vector2){square_length, square_length}, + (Color){80+array[(i*square)+(j)]*18, 0, 0, 255}); + } + } +} + +/* void draw_vertical_bargraph(game_t * game, size_t frame, int size, Color color, int * array, size_t length) { */ +/* } */ + +void draw_centered_text(game_t * game, size_t frame, int font_size, Color color, char * format, ...) { + char buffer[TEXT_BUFFER_LIMIT]; + va_list ap; + va_start(ap, format); + vsnprintf(buffer, TEXT_BUFFER_LIMIT, format, ap); + va_end(ap); + Vector2 v = MeasureTextEx(game->font, buffer, font_size, 1); + float x = game->frame_x[frame], y = game->frame_y[frame]; + DrawTextEx(game->font, buffer, (Vector2){x-v.x/2, y-v.y/2}, font_size, 1, color); +} |
