diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..a56cbd0 --- /dev/null +++ b/.clang-format @@ -0,0 +1,14 @@ +Language: Cpp +BasedOnStyle: WebKit +IndentWidth: 4 +ColumnLimit: 80 +IndentCaseLabels: true +InsertNewlineAtEOF: true +AllowShortFunctionsOnASingleLine: None + +BinPackArguments: false +AllowAllArgumentsOnNextLine: true + +BinPackParameters: false +AllowAllParametersOfDeclarationOnNextLine: true + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1fd9dda --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +./game + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3fef9c2 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ + +CFLAGS = -std=c23 +CFLAGS += -fsanitize=address,undefined -g +CFLAGS += -Wall -Wextra -Wpedantic -Wconversion -pedantic -pedantic-errors + +LFLAGS = + +CFLAGS += $(shell pkg-config sdl2 --cflags) +LFLAGS += $(shell pkg-config sdl2 --libs) + +game: main.c + gcc -o $@ $^ $(CFLAGS) $(LFLAGS) + +clean: + rm -rf game + + diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..7672925 --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,10 @@ +-xc +-std=c23 +-Wall +-Wextra +-Wpedantic +-Wconversion +-pedantic +-pedantic-errors +-Wno-unused-variable + diff --git a/main.c b/main.c new file mode 100644 index 0000000..879ae5d --- /dev/null +++ b/main.c @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +uint32_t be_to_le_u32(uint32_t be_value); +uint32_t rgba(uint32_t color); +uint32_t rgb(uint32_t color); + +int main(void) +{ + + SDL_Init(SDL_INIT_VIDEO); + + SDL_Window* window; + SDL_Renderer* renderer; + + SDL_CreateWindowAndRenderer(512, 512, 0, &window, &renderer); + // SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); + + SDL_Texture* buffer = SDL_CreateTexture(renderer, + SDL_PIXELFORMAT_RGBA32, + SDL_TEXTUREACCESS_STREAMING, + 512, + 512); + + SDL_SetTextureBlendMode(buffer, SDL_BLENDMODE_BLEND); + + while (true) { + + SDL_Event event; + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + goto exit_game; + case SDL_KEYDOWN: { + switch (event.key.keysym.sym) { + case SDLK_ESCAPE: + goto exit_game; + } + } + } + } + + SDL_RenderClear(renderer); + + uint32_t* pixels; + int row_size; + SDL_LockTexture( + buffer, &(SDL_Rect) { 0, 0, 512, 512 }, (void**)&pixels, &row_size); + row_size /= sizeof(uint32_t); + + for (int y = 0; y < 100; ++y) { + for (int x = 0; x < 100; ++x) { + pixels[(y + 100) * row_size + x + 100] = rgb(0xff0000); + } + } + + SDL_UnlockTexture(buffer); + + SDL_RenderCopy(renderer, buffer, nullptr, nullptr); + + SDL_RenderPresent(renderer); + + SDL_Delay(16); + } + +exit_game:; + printf("exitting...\n"); + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); +} + +uint32_t be_to_le_u32(uint32_t be_value) +{ + uint32_t le_value = 0; + le_value |= (be_value & 0xff) << 24; + le_value |= (be_value >> 8 & 0xff) << 16; + le_value |= (be_value >> 16 & 0xff) << 8; + le_value |= be_value >> 24 & 0xff; + return le_value; +} + +uint32_t rgba(uint32_t color) +{ + return be_to_le_u32(color); +} + +uint32_t rgb(uint32_t color) +{ + return rgba(color << 8 | 0xff); +}