Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PKGS=x11 sdl2
CFLAGS=-Wall -Wextra -std=c11 -pedantic `pkg-config --cflags $(PKGS)`
LIBS=`pkg-config --libs $(PKGS)`
LIBS=`pkg-config --libs $(PKGS)` -lm

voidf: main.c
$(CC) $(CFLAGS) -o voidf main.c $(LIBS)
107 changes: 96 additions & 11 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ Bitmap_Font bitmap_font_from_file(SDL_Renderer *renderer, const char *file_path)
return result;
}

void bitmap_font_text_size(const char *cstr, int sw, int sh, int *w, int *h)
{
*w = strlen(cstr) * BITMAP_FONT_CHAR_WIDTH * sw;
*h = BITMAP_FONT_CHAR_HEIGHT * sh;
}

void bitmap_font_render(Bitmap_Font *font,
SDL_Renderer *renderer,
int x, int y,
int w, int h,
int sw, int sh,
SDL_Color color,
const char *cstr)
{
Expand All @@ -80,10 +86,10 @@ void bitmap_font_render(Bitmap_Font *font,
for (int col = 0; (size_t) col < n; ++col) {
const SDL_Rect src_rect = bitmap_font_char_rect(font, cstr[col]);
const SDL_Rect dest_rect = {
x + BITMAP_FONT_CHAR_WIDTH * col * w,
y + BITMAP_FONT_CHAR_HEIGHT * h,
src_rect.w * w,
src_rect.h * h
x + BITMAP_FONT_CHAR_WIDTH * col * sw,
y,
src_rect.w * sw,
src_rect.h * sh
};
SDL_RenderCopy(renderer, font->bitmap, &src_rect, &dest_rect);
}
Expand Down Expand Up @@ -146,6 +152,68 @@ size_t scan_devices(Device *devices, size_t capacity)
#define DEVICES_CAPACITY 256
Device devices[DEVICES_CAPACITY];

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define FPS 60
#define DELTA_TIME (1.0f / FPS)

typedef struct {
char text[256];
float a;
} Popup;

#define POPUPS_CAPACITY 32
Popup popups[POPUPS_CAPACITY] = {0};

#define POPUP_FADEOUT_RATE 1.5f
#define POPUP_FADEOUT_DISTANCE 150
#define POPUP_SWIDTH 20
#define POPUP_SHEIGHT 20
#define POPUP_COLOR ((SDL_Color) {255, 255, 255, 255})

void popups_render(SDL_Renderer *renderer, Bitmap_Font *font)
{
for (size_t i = 0; i < POPUPS_CAPACITY; ++i) {
if (popups[i].a > 0.0) {
int w = 0;
int h = 0;
bitmap_font_text_size(popups[i].text, POPUP_SWIDTH, POPUP_SHEIGHT, &w, &h);
const int x = SCREEN_WIDTH / 2 - w / 2;
const int y = SCREEN_HEIGHT / 2 - h / 2 - (int) floorf((1.0 - popups[i].a) * POPUP_FADEOUT_DISTANCE);

SDL_Color color = POPUP_COLOR;
color.a = (Uint8) floorf(255.0f * popups[i].a);
bitmap_font_render(
font,
renderer,
x, y,
POPUP_SWIDTH, POPUP_SHEIGHT,
color,
popups[i].text);
}
}
}

void popups_update(float dt)
{
for (size_t i = 0; i < POPUPS_CAPACITY; ++i) {
if (popups[i].a > 0) {
popups[i].a -= dt * POPUP_FADEOUT_RATE;
}
}
}

void popups_new(const char *text)
{
for (size_t i = 0; i < POPUPS_CAPACITY; ++i) {
if (popups[i].a <= 0) {
snprintf(popups[i].text, sizeof(popups[i].text), "%s", text);
popups[i].a = 1.0f;
return;
}
}
}

int main()
{
const size_t devices_size = scan_devices(devices, DEVICES_CAPACITY);
Expand Down Expand Up @@ -198,7 +266,8 @@ int main()
SDL_Window * const window = SDL_CreateWindow(
"V̳͙̥̹̟͗̀̎̓͌͐́O̘̞͇̞̣͇͕͂͠I͙̋͐̍͂̀D̶͕̩̦̲͙F̟̖̮ͩ̏ͥ̂ͨ͠ ͍̰̫̯͙̯ͨ̉ͤ̈̿ͭI̤͍̲̯ͤ̎̀͝S̴̻͇̳̗̩ͧ̆ ̭̘̦ͭ͒Ĉ̸̰̼̤̖̲O̹̭̞̺̻͚̣̒M̪͓̗̤͋͢Ĩ͔̗̣̻̄̏̏̏̚N̳̦̂ͯ̅͂̓̈́G͈̣",
0, 0,
800, 600, SDL_WINDOW_RESIZABLE);
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_RESIZABLE);
if (window == NULL) {
fprintf(stderr, "ERROR: Could not initialize SDL: %s\n", SDL_GetError());
exit(1);
Expand All @@ -211,6 +280,10 @@ int main()
exit(1);
}

SDL_RenderSetLogicalSize(renderer,
(int) SCREEN_WIDTH,
(int) SCREEN_HEIGHT);

// TODO(#3): charmap-oldschool.bmp should be baked into the executable
Bitmap_Font font = bitmap_font_from_file(renderer, "./charmap-oldschool.bmp");

Expand Down Expand Up @@ -239,25 +312,37 @@ int main()
if(is_voidf(&cursor, ev[i].code)) {
printf("VOIDF IS COMING\n");
voidf_count += 1;
// TODO(#6): quake-style combo message
popups_new("voidf");
}
}
}
}

SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
popups_update(DELTA_TIME);

SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);

snprintf(voidf_buffer, sizeof(voidf_buffer), "%d", voidf_count);

// TODO(#4): voidf counter is ugly
const int sw = 10 * 3;
const int sh = 10 * 3;
int w = 0;
int h = 0;
bitmap_font_text_size(voidf_buffer, sw, sh, &w, &h);
const int x = SCREEN_WIDTH / 2 - w / 2;
const int y = SCREEN_HEIGHT / 2 - h / 2;
bitmap_font_render(&font,
renderer,
0, 0,
10, 10,
x, y,
sw, sh,
(SDL_Color) {255, 255, 255, 255},
voidf_buffer);

SDL_Delay(10);
popups_render(renderer, &font);

SDL_Delay((int) floorf(DELTA_TIME * 1000.0f));
SDL_RenderPresent(renderer);
}

Expand Down