I want to handle different resolutions without altering any game objects. I tried using SDL_RenderSetLogicalSize()
but I don't know how to implement it properly. Providing an example of implementing it would be really helpful (in C). I saw a similar question asked here years ago but I didn't understand that. I'm a beginner building my first project involving a few rectangles. This is my current code:
#include <stdio.h>
#include <SDL.h>
#define WINDOW_HEIGHT 480//p
#define WINDOW_WIDTH (16 * (WINDOW_HEIGHT / 9))
#define FRAME_TARGET_TIME (1000 / 75 /*FPS*/)
int last_frame_time = 0;
SDL_bool is_running = SDL_FALSE;
SDL_Window* window = NULL;
SDL_Window* window2 = NULL;
SDL_Renderer* renderer = NULL;
int xA = 0, yA = 0, wA = 100, hA = 75, vA = 1000;
int xB = 0, yB = 0, wB = 100, hB = 75, vB = 1000;
SDL_bool initialize_window(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
fprintf(stderr, "Error initializing SDL.\n");
return SDL_FALSE;
}
window = SDL_CreateWindow(NULL, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_BORDERLESS);
if (!window) {
fprintf(stderr, "Error creating SDL Window.\n");
return SDL_FALSE;
}
window2 = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_HIDDEN);
if (!window2) {
fprintf(stderr, "Error creating SDL Window2.\n");
return SDL_FALSE;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
fprintf(stderr, "Error creating SDL Renderer.\n");
return SDL_FALSE;
}
SDL_RenderSetLogicalSize(renderer, 1920, 1080);
return SDL_TRUE;
}
void process_input(void) {
SDL_Event event;
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
is_running = SDL_FALSE;
}
else if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
is_running = SDL_FALSE;
break;
case SDLK_SPACE:
break;
case SDLK_LEFT:
break;
case SDLK_RIGHT:
break;
default:
break;
}
}
else if (event.type == SDL_KEYUP) {
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
break;
case SDLK_SPACE:
break;
case SDLK_LEFT:
vA = -1000;
break;
case SDLK_RIGHT:
vA = 1000;
break;
default:
break;
}
}
}
}
void setup(void) {
}
void update(void) {
int time_to_wait = FRAME_TARGET_TIME - (SDL_GetTicks() - last_frame_time);
if (time_to_wait > 0 && time_to_wait <= FRAME_TARGET_TIME) SDL_Delay(time_to_wait);
float delta_time = (SDL_GetTicks() - last_frame_time) / 1000.0;
last_frame_time = SDL_GetTicks();
xA += vA * delta_time;
if (xA < -wA) xA = WINDOW_WIDTH + wA;
if (xA > WINDOW_WIDTH + wA) xA = -wA;
}
void render(void) {
SDL_SetRenderDrawColor(renderer, 0x0, 0x0, 0x0, 0x0);
SDL_RenderClear(renderer);
SDL_Rect rectangleA = { xA, yA, wA, hA };
SDL_Rect rectangleB = { xB, yB, wB, hB };
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0x0);
SDL_RenderFillRect(renderer, &rectangleA);
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0x0);
SDL_RenderFillRect(renderer, &rectangleB);
SDL_RenderPresent(renderer);
}
void destroy_window(void) {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_DestroyWindow(window2);
SDL_Quit();
}
int main(int argc, char* args[]) {
xA = (WINDOW_WIDTH - wA) / 2;
yA = (WINDOW_HEIGHT - hA) / 2;
is_running = initialize_window();
setup();
while (is_running) {
process_input();
update();
render();
}
destroy_window();
return 0;
}```