Render string on screen

This commit is contained in:
Joseph Ferano 2023-09-19 23:31:05 +07:00
parent 868922c68e
commit b1ab0f6d98
2 changed files with 65 additions and 34 deletions

View File

@ -6,7 +6,7 @@ CC = g++
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -w
COMPILER_FLAGS = -Wall -Wpedantic
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2 -lSDL2_image

View File

@ -21,6 +21,60 @@ enum KeyPressSurfaces
KEY_PRESS_SURFACE_TOTAL
};
SDL_Rect get_srcrect(char key) {
SDL_Rect srcrect;
srcrect.x = (key % 16) * 70;
srcrect.y = (key / 16) * 80;
srcrect.w = 80;
srcrect.h = 80;
return srcrect;
}
void render_char(char key, int row, int col, SDL_Renderer* renderer, SDL_Texture* font) {
SDL_Rect dstrect;
dstrect.x = col;
dstrect.y = row;
dstrect.w = 80;
dstrect.h = 80;
SDL_Rect srcrect = get_srcrect(key);
SDL_RenderCopy(renderer, font, &srcrect, &dstrect);
}
void Draw(char keyPressed, SDL_Renderer* renderer, SDL_Texture* fontTexture) {
SDL_Rect srcrect;
SDL_Rect dstrect;
if (keyPressed > 0) {
srcrect.x = (keyPressed % 16) * 70;
srcrect.y = (keyPressed / 16) * 80;
} else {
srcrect.x = 0;
srcrect.y = 0;
}
srcrect.w = 80;
srcrect.h = 80;
dstrect.x = SCREEN_WIDTH/2 - 40;
dstrect.y = SCREEN_HEIGHT/2 - 40;
dstrect.w = 100;
dstrect.h = 100;
// SDL_BlitSurface(imageSurface, &srcrect, screenSurface, &dstrect);
//Update the surface
// SDL_UpdateWindowSurface( window );
SDL_RenderCopy(renderer, fontTexture, &srcrect, &dstrect);
}
int main(int argc, char *argv[]) {
SDL_Window* window = NULL;
@ -40,8 +94,7 @@ int main(int argc, char *argv[]) {
return 1;
}
SDL_Renderer* renderer =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 130, 163, 255, 1);
@ -58,10 +111,12 @@ int main(int argc, char *argv[]) {
//Fill the surface white
// auto rgbMap = SDL_MapRGB( screenSurface->format, 0x82, 0xA3, 0xFF);
int test = 0;
SDL_Event e;
bool quit = false;
int keyPressed = -1;
int rowCurr = 0;
int colCurr = 0;
string text = "Some text";
while (quit == false) {
while (SDL_PollEvent( &e)) {
if (e.type == SDL_QUIT) {
@ -77,38 +132,14 @@ int main(int argc, char *argv[]) {
// }
}
}
// SDL_FillRect( screenSurface, NULL, rgbMap);
SDL_Rect srcrect;
SDL_Rect dstrect;
if (keyPressed > 0) {
srcrect.x = (keyPressed % 16) * 70;
srcrect.y = (keyPressed / 16) * 80;
} else {
srcrect.x = 0;
srcrect.y = 0;
}
srcrect.w = 80;
srcrect.h = 80;
dstrect.x = SCREEN_WIDTH/2 - 40;
dstrect.y = SCREEN_HEIGHT/2 - 40;
dstrect.w = 100;
dstrect.h = 100;
// SDL_BlitSurface(imageSurface, &srcrect, screenSurface, &dstrect);
//Update the surface
// SDL_UpdateWindowSurface( window );
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, fontTexture, &srcrect, &dstrect);
SDL_SetTextureColorMod(fontTexture, 0xFF, 0, 0);
for (int i = 0; i < 10; i++) {
render_char(text[i], rowCurr, colCurr++ * 50, renderer, fontTexture);
}
colCurr = 0;
// Draw(keyPressed, renderer, fontTexture);
SDL_RenderPresent(renderer);
}