Compare commits
No commits in common. "dc5d252c904afcc3ed88beb7eff7e815b157698d" and "883773d07c05a75171d4ae8d501452d6d1e6befb" have entirely different histories.
dc5d252c90
...
883773d07c
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +0,0 @@
|
|||||||
.cache
|
|
||||||
compile_commands.json
|
|
||||||
main
|
|
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +0,0 @@
|
|||||||
[submodule "intern/aliases"]
|
|
||||||
path = intern/aliases
|
|
||||||
url = https://git.thewizardapprentice.com/abdelrahman/c-cpp-aliases.git
|
|
@ -1,3 +0,0 @@
|
|||||||
# SDL Boilerplate
|
|
||||||
|
|
||||||
Boilerplate for setting up a simple SDL program that creates a window and a renderer and handles the quit event.
|
|
9
compile
9
compile
@ -1,9 +0,0 @@
|
|||||||
#!/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)
|
|
@ -1,8 +0,0 @@
|
|||||||
#ifndef COMPOSITOR_H
|
|
||||||
#define COMPOSITOR_H
|
|
||||||
|
|
||||||
#include "aliases/aliases.h"
|
|
||||||
|
|
||||||
i32 run_main_loop(void);
|
|
||||||
|
|
||||||
#endif // !COMPOSITOR_H
|
|
@ -1,56 +0,0 @@
|
|||||||
#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 +0,0 @@
|
|||||||
Subproject commit f95f3aa499910286876c1f2cdceea8146ebcf7b1
|
|
47
main.c
Normal file
47
main.c
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#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;
|
||||||
|
}
|
@ -1,59 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
#include "aliases/aliases.h"
|
|
||||||
#include "compositor.h"
|
|
||||||
|
|
||||||
i32 main(void) { return run_main_loop(); }
|
|
74
src/window.c
74
src/window.c
@ -1,74 +0,0 @@
|
|||||||
#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