This commit is contained in:
2025-01-30 15:22:02 +01:00
commit b7e5e2aa35
9 changed files with 163 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
cmake-build-debug

8
.idea/.gitignore generated vendored Normal file
View File

@@ -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

7
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakePythonSetting">
<option name="pythonIntegrationState" value="YES" />
</component>
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/sdlzasedaco.iml" filepath="$PROJECT_DIR$/.idea/sdlzasedaco.iml" />
</modules>
</component>
</project>

2
.idea/sdlzasedaco.iml generated Normal file
View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

11
CMakeLists.txt Normal file
View File

@@ -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)

120
main.c Normal file
View File

@@ -0,0 +1,120 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <stdlib.h>
//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;
}

BIN
rasterthingy.ttf Normal file

Binary file not shown.