Compare commits

..

10 Commits

14 changed files with 374 additions and 49 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
.cache
.vscode
frames
bahnschrift.ttf
compile_commands.json
player

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "intern/aliases"]
path = intern/aliases
url = https://git.thewizardapprentice.com/abdelrahman/c-cpp-aliases.git

View File

@ -1,3 +1,5 @@
# SDL Boilerplate # Image sequence player
Boilerplate for setting up a simple SDL program that creates a window and a renderer and handles the quit event. Test to create an image sequence player with ability to change playback speed
![Result](./demo.mp4)

3
build Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
bear -- ./compile

9
compile Executable file
View File

@ -0,0 +1,9 @@
#!/bin/bash
CC=clang
CFLAGS="-g -I./intern -I./include $(pkg-config --cflags sdl2 SDL2_image SDL2_ttf)"
LIBS="$(pkg-config --libs sdl2 SDL2_image SDL2_ttf) -lm"
SRC=src/*.c
OUT=player
(set -x ; $CC $CFLAGS $LIBS $SRC -o $OUT)

BIN
demo.mp4 Normal file

Binary file not shown.

38
include/fps.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef FPS_H
#define FPS_H
#include "SDL_render.h"
enum fps_indices {
FPS_12,
FPS_23_976,
FPS_24,
FPS_25,
FPS_29_97,
FPS_30,
FPS_48,
FPS_50,
FPS_59_94,
FPS_60,
COUNT_FPS,
};
// clang-format off
const char *fps_names[] = {
[FPS_12] = "12.0",
[FPS_23_976] = "23.976",
[FPS_24] = "24.0",
[FPS_25] = "25.0",
[FPS_29_97] = "29.97",
[FPS_30] = "30.0",
[FPS_48] = "48.0",
[FPS_50] = "50.0",
[FPS_59_94] = "59.94",
[FPS_60] = "60.0",
};
// clang-format on
SDL_Texture *fps_textures[COUNT_FPS] = {0};
SDL_Texture *fps_textures_selected[COUNT_FPS] = {0};
#endif // !FPS_H

17
include/img_seq.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef IMG_SEQ_H
#define IMG_SEQ_H
#include "SDL_render.h"
#include "aliases/aliases.h"
typedef struct {
u64 pos;
u64 length;
SDL_Texture *frames[];
} img_seq_t;
img_seq_t *create_sequence(u64 frame_count);
SDL_Texture *current_frame(img_seq_t *seq);
void next_frame(img_seq_t *seq);
#endif // !IMG_SEQ_H

28
include/ui_list.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef UI_LIST_H
#define UI_LIST_H
#include "SDL_pixels.h"
#include "SDL_render.h"
#include "aliases/aliases.h"
typedef struct {
union {
u32 colour;
SDL_Color rgba;
};
} colour_t;
typedef struct {
u64 item_w;
u64 item_h;
u64 selected;
u64 length;
SDL_Texture **textures;
SDL_Texture **selected_textures;
} ui_list_t;
u64 ui_list(const ui_list_t *list, u64 x, u64 y, u64 padding, i64 mouse_x,
i64 mouse_y, i64 clicked_x, i64 clicked_y, SDL_Renderer *renderer,
colour_t bg_colour, colour_t hover_colour);
#endif // !UI_LIST_H

1
intern/aliases Submodule

@ -0,0 +1 @@
Subproject commit f95f3aa499910286876c1f2cdceea8146ebcf7b1

47
main.c
View File

@ -1,47 +0,0 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h>
#include <stdbool.h>
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
int main(void) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window =
SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(
window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
bool running = true;
SDL_Event event = {};
while (running) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
}
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

17
src/img_seq.c Normal file
View File

@ -0,0 +1,17 @@
#include "img_seq.h"
#include "aliases/aliases.h"
img_seq_t *create_sequence(u64 frame_count) {
return (img_seq_t *)malloc(
(sizeof(img_seq_t) + frame_count * sizeof(SDL_Texture *)));
}
SDL_Texture *current_frame(img_seq_t *seq) { return seq->frames[seq->pos]; }
void next_frame(img_seq_t *seq) {
++(seq->pos);
if (seq->pos >= seq->length) {
seq->pos = 0;
}
}

170
src/main.c Normal file
View File

@ -0,0 +1,170 @@
#include "SDL_rect.h"
#include "SDL_surface.h"
#include "SDL_timer.h"
#include "aliases/aliases.h"
#include "fps.h"
#include "img_seq.h"
#include "ui_list.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_video.h>
#include <linux/limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
INTERNAL colour_t bg_colour = {.colour = 0xff4c3715};
INTERNAL colour_t hover_colour = {.colour = 0xff5e4a18};
INTERNAL colour_t normal_text = {.colour = 0xff000000};
INTERNAL colour_t selected_text = {.colour = 0xffd8e6e8};
u64 max(u64 a, u64 b) { return a > b ? a : b; }
u64 digit_count(f64 num) { return (u64)log10(num) + 1; }
int main(void) {
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_TIF);
TTF_Init();
SDL_Window *window =
SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(
window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
TTF_Font *font = TTF_OpenFont("./bahnschrift.ttf", 24);
bool running = true;
SDL_Event event = {0};
f32 fps[COUNT_FPS] = {0};
for (u64 i = 0; i < COUNT_FPS; ++i) {
fps[i] = atof(fps_names[i]);
}
ui_list_t fps_ui = {
.item_w = 0,
.item_h = 0,
.selected = FPS_12,
.length = COUNT_FPS,
.textures = fps_textures,
.selected_textures = fps_textures_selected,
};
for (u64 i = 0; i < COUNT_FPS; ++i) {
SDL_Surface *surface =
TTF_RenderText_Solid(font, fps_names[i], normal_text.rgba);
fps_textures[i] = SDL_CreateTextureFromSurface(renderer, surface);
fps_ui.item_w = max(surface->w, fps_ui.item_w);
fps_ui.item_h = max(surface->h, fps_ui.item_h);
SDL_FreeSurface(surface);
surface = TTF_RenderText_Solid(font, fps_names[i], selected_text.rgba);
fps_textures_selected[i] = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
}
f64 wait = 1000.0f / fps[fps_ui.selected];
u64 frame_count = 138;
img_seq_t *seq = create_sequence(frame_count);
seq->pos = 0;
seq->length = frame_count;
for (u64 i = 0; i < frame_count; ++i) {
char frame[10];
char path[PATH_MAX];
u64 length = i == 0 ? 1 : digit_count(i);
sprintf(path, "./frames/render_");
for (u64 j = 0; j < 4 - length; ++j) {
strcat(path, "0");
}
sprintf(frame, "%ld.tif", i);
strcat(path, frame);
seq->frames[i] = IMG_LoadTexture(renderer, path);
}
i32 y_pos = 50;
u64 padding = 5;
i64 mouse_x;
i64 mouse_y;
i64 clicked_x;
i64 clicked_y;
SDL_Rect dst = {.x = 150, .y = y_pos, .w = 960, .h = 540};
while (running) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
case SDL_MOUSEMOTION:
mouse_x = event.motion.x;
mouse_y = event.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
clicked_x = event.button.x;
clicked_y = event.button.y;
break;
}
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, current_frame(seq), NULL, &dst);
fps_ui.selected =
ui_list(&fps_ui, 40, y_pos + padding, padding, mouse_x, mouse_y,
clicked_x, clicked_y, renderer, bg_colour, hover_colour);
clicked_x = clicked_y = -1;
wait = 1000.0 / fps[fps_ui.selected];
next_frame(seq);
SDL_RenderPresent(renderer);
SDL_Delay(wait);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
IMG_Quit();
SDL_Quit();
return 0;
}

78
src/ui_list.c Normal file
View File

@ -0,0 +1,78 @@
#include "ui_list.h"
#include "SDL_rect.h"
#include "SDL_render.h"
#include "aliases/aliases.h"
#include <stdbool.h>
#include <stdio.h>
bool aabb(SDL_Rect *rect, i64 x, i64 y) {
return x >= rect->x && y >= rect->y && x < rect->x + rect->w &&
y < rect->y + rect->h;
}
bool draw_element(SDL_Texture *texture, SDL_Texture *selected_texture,
bool selected, u64 x, u64 y, u64 item_w, u64 item_h,
u64 padding, i64 mouse_x, i64 mouse_y, i64 clicked_x,
i64 clicked_y, SDL_Renderer *renderer, colour_t bg_colour,
colour_t hover_colour) {
bool output = false;
SDL_Rect bg_dest = {.x = x - padding,
.y = y - padding,
.w = item_w + padding * 2,
.h = item_h + padding * 2 + 1};
SDL_Rect texture_dest = {.x = x, .y = y, .w = 0, .h = 0};
colour_t c = bg_colour;
bool hovered = false;
if (aabb(&bg_dest, mouse_x, mouse_y)) {
hovered = true;
c = hover_colour;
}
if (aabb(&bg_dest, clicked_x, clicked_y)) {
output = true;
}
SDL_SetRenderDrawColor(renderer, c.rgba.r, c.rgba.g, c.rgba.b, c.rgba.a);
SDL_Texture *render_texture =
(selected || hovered) ? selected_texture : texture;
i32 texture_w;
i32 texture_h;
SDL_QueryTexture(render_texture, NULL, NULL, &texture_w, &texture_h);
i32 x_offset = (item_w - texture_w) / 2;
texture_dest.x += x_offset;
texture_dest.w = texture_w;
texture_dest.h = texture_h;
(selected || hovered) ? SDL_RenderFillRect(renderer, &bg_dest)
: SDL_RenderDrawRect(renderer, &bg_dest);
SDL_RenderCopy(renderer, render_texture, NULL, &texture_dest);
return output;
}
u64 ui_list(const ui_list_t *list, u64 x, u64 y, u64 padding, i64 mouse_x,
i64 mouse_y, i64 clicked_x, i64 clicked_y, SDL_Renderer *renderer,
colour_t bg_colour, colour_t hover_colour) {
u64 selected = list->selected;
for (u64 i = 0; i < list->length; ++i) {
if (draw_element(list->textures[i], list->selected_textures[i],
i == list->selected, x,
y + (list->item_h + padding * 2) * i, list->item_w,
list->item_h, padding, mouse_x, mouse_y, clicked_x,
clicked_y, renderer, bg_colour, hover_colour)) {
selected = i;
}
}
return selected;
}