Compare commits
10 Commits
883773d07c
...
dc5d252c90
Author | SHA1 | Date | |
---|---|---|---|
dc5d252c90 | |||
150ada41b2 | |||
ea3aceeaf8 | |||
04a666e3b5 | |||
82b8f88897 | |||
8ad090b582 | |||
0ed130b659 | |||
99c6dbeecf | |||
41f12d2a98 | |||
8c27f2b770 |
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.cache
|
||||||
|
compile_commands.json
|
||||||
|
main
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[submodule "intern/aliases"]
|
||||||
|
path = intern/aliases
|
||||||
|
url = https://git.thewizardapprentice.com/abdelrahman/c-cpp-aliases.git
|
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# SDL Boilerplate
|
||||||
|
|
||||||
|
Boilerplate for setting up a simple SDL program that creates a window and a renderer and handles the quit event.
|
9
compile
Executable file
9
compile
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
CC=clang
|
||||||
|
CFLAGS="-g -Wall -Iinclude -Iintern $(pkg-config --cflags sdl2)"
|
||||||
|
LIBS="$(pkg-config --libs sdl2)"
|
||||||
|
SRC=src/*.c
|
||||||
|
OUT=main
|
||||||
|
|
||||||
|
(set -x ; $CC $CFLAGS $LIBS $SRC -o $OUT)
|
8
include/compositor.h
Normal file
8
include/compositor.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef COMPOSITOR_H
|
||||||
|
#define COMPOSITOR_H
|
||||||
|
|
||||||
|
#include "aliases/aliases.h"
|
||||||
|
|
||||||
|
i32 run_main_loop(void);
|
||||||
|
|
||||||
|
#endif // !COMPOSITOR_H
|
56
include/window.h
Normal file
56
include/window.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#ifndef WINDOW_H
|
||||||
|
#define WINDOW_H
|
||||||
|
|
||||||
|
#include "SDL_pixels.h"
|
||||||
|
#include "aliases/aliases.h"
|
||||||
|
#include <SDL2/SDL_render.h>
|
||||||
|
#include <SDL2/SDL_video.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct point point_t;
|
||||||
|
typedef struct line line_t;
|
||||||
|
typedef struct triangle triangle_t;
|
||||||
|
typedef struct window window_t;
|
||||||
|
|
||||||
|
struct point {
|
||||||
|
i32 x;
|
||||||
|
i32 y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct line {
|
||||||
|
point_t p0;
|
||||||
|
point_t p1;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct triangle {
|
||||||
|
point_t p0;
|
||||||
|
point_t p1;
|
||||||
|
point_t p2;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct window {
|
||||||
|
u64 width;
|
||||||
|
u64 height;
|
||||||
|
const char *title;
|
||||||
|
SDL_Window *window;
|
||||||
|
SDL_Renderer *renderer;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct colour colour_t;
|
||||||
|
struct colour {
|
||||||
|
union {
|
||||||
|
u32 abgr;
|
||||||
|
SDL_Color colour;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
bool init_window(window_t *wnd, const char *title, u64 width, u64 height);
|
||||||
|
void cleanup_window(window_t *wnd);
|
||||||
|
void clear_window(const window_t *wnd, colour_t colour);
|
||||||
|
void swap_buffers(const window_t *wnd);
|
||||||
|
void draw_point(const window_t *wnd, point_t p, colour_t colour);
|
||||||
|
void draw_line(const window_t *wnd, const line_t *ln, colour_t colour);
|
||||||
|
void draw_triangle(const window_t *wnd, const triangle_t *triangle,
|
||||||
|
colour_t colour);
|
||||||
|
|
||||||
|
#endif // !WINDOW_H
|
1
intern/aliases
Submodule
1
intern/aliases
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit f95f3aa499910286876c1f2cdceea8146ebcf7b1
|
47
main.c
47
main.c
@ -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;
|
|
||||||
}
|
|
59
src/compositor.c
Normal file
59
src/compositor.c
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#include "aliases/aliases.h"
|
||||||
|
#include "window.h"
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_events.h>
|
||||||
|
#include <SDL2/SDL_render.h>
|
||||||
|
#include <SDL2/SDL_video.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define MAX_WINDOWS 2
|
||||||
|
|
||||||
|
#define WINDOW_WIDTH 800
|
||||||
|
#define WINDOW_HEIGHT 600
|
||||||
|
|
||||||
|
typedef struct compositor compositor_t;
|
||||||
|
struct compositor {
|
||||||
|
window_t windows[MAX_WINDOWS];
|
||||||
|
bool running;
|
||||||
|
SDL_Event event;
|
||||||
|
};
|
||||||
|
|
||||||
|
i32 run_main_loop(void) {
|
||||||
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
compositor_t comp = {0};
|
||||||
|
|
||||||
|
window_t *main_window = &(comp.windows[0]);
|
||||||
|
|
||||||
|
if (!init_window(main_window, "Compositor", WINDOW_WIDTH, WINDOW_HEIGHT)) {
|
||||||
|
SDL_Quit();
|
||||||
|
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
comp.running = true;
|
||||||
|
|
||||||
|
colour_t bg_colour = {.abgr = 0xffffffff};
|
||||||
|
colour_t fg_colour = {.abgr = 0xff000000};
|
||||||
|
|
||||||
|
while (comp.running) {
|
||||||
|
while (SDL_PollEvent(&(comp.event))) {
|
||||||
|
switch (comp.event.type) {
|
||||||
|
case SDL_QUIT:
|
||||||
|
comp.running = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clear_window(main_window, bg_colour);
|
||||||
|
|
||||||
|
swap_buffers(main_window);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Quit();
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
4
src/main.c
Normal file
4
src/main.c
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
#include "aliases/aliases.h"
|
||||||
|
#include "compositor.h"
|
||||||
|
|
||||||
|
i32 main(void) { return run_main_loop(); }
|
74
src/window.c
Normal file
74
src/window.c
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#include "window.h"
|
||||||
|
#include "SDL_render.h"
|
||||||
|
#include "SDL_video.h"
|
||||||
|
#include "aliases/aliases.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
bool init_window(window_t *wnd, const char *title, u64 width, u64 height) {
|
||||||
|
wnd->window =
|
||||||
|
SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||||
|
width, height, SDL_WINDOW_SHOWN);
|
||||||
|
if (!(wnd->window)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
wnd->renderer = SDL_CreateRenderer(
|
||||||
|
wnd->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||||
|
if (!(wnd->renderer)) {
|
||||||
|
cleanup_window(wnd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
wnd->title = title;
|
||||||
|
wnd->width = width;
|
||||||
|
wnd->height = height;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup_window(window_t *wnd) {
|
||||||
|
if (wnd->renderer) {
|
||||||
|
SDL_DestroyRenderer(wnd->renderer);
|
||||||
|
wnd->renderer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wnd->window) {
|
||||||
|
SDL_DestroyWindow(wnd->window);
|
||||||
|
wnd->window = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
wnd->width = wnd->height = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_colour(const window_t *wnd, colour_t colour) {
|
||||||
|
SDL_SetRenderDrawColor(wnd->renderer, colour.colour.r, colour.colour.g,
|
||||||
|
colour.colour.b, colour.colour.a);
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear_window(const window_t *wnd, colour_t colour) {
|
||||||
|
set_colour(wnd, colour);
|
||||||
|
SDL_RenderClear(wnd->renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap_buffers(const window_t *wnd) { SDL_RenderPresent(wnd->renderer); }
|
||||||
|
|
||||||
|
void draw_point(const window_t *wnd, point_t p, colour_t colour) {
|
||||||
|
set_colour(wnd, colour);
|
||||||
|
SDL_RenderDrawPoint(wnd->renderer, p.x, p.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw_line(const window_t *wnd, const line_t *ln, colour_t colour) {
|
||||||
|
set_colour(wnd, colour);
|
||||||
|
SDL_RenderDrawLine(wnd->renderer, ln->p0.x, ln->p0.y, ln->p1.x, ln->p1.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw_triangle(const window_t *wnd, const triangle_t *triangle,
|
||||||
|
colour_t colour) {
|
||||||
|
line_t ln0 = {triangle->p0, triangle->p1};
|
||||||
|
line_t ln1 = {triangle->p0, triangle->p2};
|
||||||
|
line_t ln2 = {triangle->p1, triangle->p2};
|
||||||
|
|
||||||
|
draw_line(wnd, &ln0, colour);
|
||||||
|
draw_line(wnd, &ln1, colour);
|
||||||
|
draw_line(wnd, &ln2, colour);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user