commit b7e5e2aa3517fe737dfcf41170fe78a1183d612e Author: bruno Date: Thu Jan 30 15:22:02 2025 +0100 Init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bf797c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cmake-build-debug \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0b76fe5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9957783 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sdlzasedaco.iml b/.idea/sdlzasedaco.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/sdlzasedaco.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..efca100 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.31) +project(sdlzasedaco C) + +set(CMAKE_C_STANDARD 23) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(SDL2 REQUIRED sdl2) + +add_executable(sdlzasedaco main.c) # Ensure the target is defined before linking + +target_link_libraries(sdlzasedaco SDL2 SDL2_ttf) diff --git a/main.c b/main.c new file mode 100644 index 0000000..3ec13a6 --- /dev/null +++ b/main.c @@ -0,0 +1,120 @@ +#include +#include +#include +#include + +//Screen dimension constants +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; + +void renderText(SDL_Renderer *renderer, TTF_Font *font, char *string, uint16_t x, uint16_t y, uint8_t r, uint8_t g, + uint8_t b, uint8_t a) { + SDL_Texture *fontTempTex; + SDL_Surface *fontTempSurf; + SDL_Color color = {r, g, b, a}; + fontTempSurf = TTF_RenderText_Blended(font, string, color); + fontTempTex = SDL_CreateTextureFromSurface(renderer, fontTempSurf); + + int iW, iH; + SDL_QueryTexture(fontTempTex, NULL, NULL, &iW, &iH); + + SDL_Rect srcRect; + srcRect.x = 0; + srcRect.y = 0; + srcRect.w = iW; + srcRect.h = iH; + + SDL_Rect dstRect; + dstRect.x = x; + dstRect.y = y; + dstRect.w = iW; + dstRect.h = iH; + + SDL_RenderCopy(renderer, fontTempTex, &srcRect, &dstRect); + + SDL_FreeSurface(fontTempSurf); + SDL_DestroyTexture(fontTempTex); +} + +int main(int argc, char *args[]) { + //The window we'll be rendering to + SDL_Window *window = NULL; + + //The surface contained by the window + SDL_Renderer *renderer = NULL; + + SDL_Rect rect1; + + TTF_Font *gFont; + + + //Initialize SDL + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + return 1; + } + + //Initialize SDL_ttf + if (TTF_Init() == -1) { + printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError()); + return 1; + } + + gFont = TTF_OpenFont("../rasterthingy.ttf", 50); + if (gFont == NULL) { + printf("Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError()); + return 1; + } + + //Create window + window = SDL_CreateWindow("SDLko", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, + SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + if (window == NULL) { + printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); + return 1; + } + //Get window surface + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + + //Hack to get window to stay up + SDL_Event e; + bool quit = false; + Uint64 start; + Uint64 end; + while (!quit) { + start = SDL_GetTicks64(); + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) quit = true; + } + + + SDL_SetRenderDrawColor(renderer, 128, 0, 0, 255); + SDL_RenderClear(renderer); + + SDL_SetRenderDrawColor(renderer, 0, 128, 0, 255); + rect1.x = (rect1.x + 1) % 400; + rect1.y = 10; + rect1.w = 50; + rect1.h = 10; + SDL_RenderFillRect(renderer, &rect1); + + char textTemp[12]; + sprintf(textTemp, "%d", rect1.x); + + renderText(renderer, gFont, textTemp, 100, 100, 255, 255, 255, 255); + + SDL_RenderPresent(renderer); + end = SDL_GetTicks64(); + SDL_Delay((1000 / 60) - (end - start)); + } + + TTF_CloseFont(gFont); + + //Destroy window + SDL_DestroyWindow(window); + + //Quit SDL subsystems + SDL_Quit(); + + return 0; +} \ No newline at end of file diff --git a/rasterthingy.ttf b/rasterthingy.ttf new file mode 100644 index 0000000..8d80982 Binary files /dev/null and b/rasterthingy.ttf differ