Retina Display Support with SDL - c

I can't find a single useful thing from any Google search about SDL (2.0) supporting retina displays. Could anyone point me in the right direction?
I'm using this code:
//Start SDL
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *screen = NULL;
SDL_Renderer *render = NULL;
SDL_Texture *texture = NULL;
SDL_Event e;
bool quit = false;
screen = SDL_CreateWindow("Sample Image",100,100,500,500,SDL_WINDOW_SHOWN);
render = SDL_CreateRenderer(screen,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
texture = IMG_LoadTexture(render,"image.bmp");
if (texture == NULL)
{
printf("%s",IMG_GetError());
}
//Render all graphics
SDL_RenderClear(render);
SDL_RenderCopy(render,texture,NULL,NULL);
SDL_RenderPresent(render);
while (quit == false) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT)
quit = true;
}
}
//Remove all graphics from memory
SDL_DestroyRenderer(render);
SDL_DestroyTexture(texture);
SDL_DestroyWindow(screen);
//Quit SDL
SDL_Quit();

SDL_Window* win = SDL_CreateWindow("test",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 480,
SDL_WINDOW_RESIZABLE|SDL_WINDOW_OPENGL|SDL_WINDOW_ALLOW_HIGHDPI);
the "SDL_WINDOW_ALLOW_HIGHDPI" flag can do it for you.

Related

Wrong color with SDL on HDR image?

I have my program that is displaying my image:
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
void Clean_ressources(SDL_Window *w, SDL_Renderer *r, SDL_Texture *t)
{
if (t!= NULL)
SDL_DestroyTexture(t);
if (t!= NULL)
SDL_DestroyRenderer(r);
if (w!= NULL)
SDL_DestroyWindow(w);
SDL_Quit();
}
void SDL_ExitWithError(const char *message);
int main(int argc, char **argv)
{
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Surface *surface = NULL;
SDL_Texture *texture, *tmp = NULL;
SDL_Rect dest_rest = {0, 0, 640, 480};
//SDL Init
if(SDL_Init(SDL_INIT_VIDEO) != 0)
SDL_ExitWithError("SDL init failed");
//Create Window
window = SDL_CreateWindow("My Viewer 10 bits", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED ,1000, 1000, 0);
if(window == NULL)
{
Clean_ressources(NULL, NULL, NULL);
SDL_ExitWithError("Window creation failed");
}
//Create Renderer
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
if(renderer == NULL)
{
Clean_ressources(window, NULL, NULL);
SDL_ExitWithError("Renderer creation failed");
}
//Load image
// https://github.com/codelogic/wide-gamut-tests/blob/master/R2020-sRGB-red.jpg
surface = IMG_Load("src/red.jpg");
if(surface == NULL)
{
Clean_ressources(window, renderer, NULL);
SDL_ExitWithError("Load image failed");
}
tmp = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
if(tmp == NULL)
{
Clean_ressources(window, renderer, NULL);
SDL_ExitWithError("Create texture from surface failed");
}
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB2101010, SDL_TEXTUREACCESS_TARGET, surface->w, surface->h);
if(texture == NULL)
{
Clean_ressources(window, renderer, NULL);
SDL_ExitWithError("Create texture failed");
}
if(SDL_QueryTexture(texture, NULL, NULL, &dest_rest.w, &dest_rest.h) != 0)
{
Clean_ressources(window, renderer, NULL);
SDL_ExitWithError("Query texture failed");
}
if(SDL_RenderCopy(renderer, tmp, NULL, NULL) != 0)
{
Clean_ressources(window, renderer, NULL);
SDL_ExitWithError("Render copy failed");
}
SDL_SetRenderTarget(renderer, texture);
SDL_RenderPresent(renderer);
SDL_DestroyTexture(tmp);
SDL_FreeSurface(surface);
SDL_SetRenderTarget(renderer, NULL);
/*--------------------------------------------------------------*/
SDL_bool program_launched = SDL_TRUE;
while(program_launched)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
program_launched = SDL_FALSE;
break;
default:
break;
}
}
}
/*--------------------------------------------------------------*/
Clean_ressources(window, renderer, texture);
SDL_Quit();
return EXIT_SUCCESS;
}
void SDL_ExitWithError(const char *message)
{
SDL_Log("ERROR :%s > %s\n", message, SDL_GetError());
exit(EXIT_FAILURE);
}
I tried with different image and everything seems fine, but with this image I don't understand the color is very different in my window.
Here is the image in input:
And this is the image in my window in SDL2:
I'm aware that this image is not a high dynamic range color but I would like to display in my HDR screen an image that have HDR color (10 bits depth color) in SDL but if my sdl window have color problem I won't be able to do that.
I also tried with SDL_PIXELFORMAT_RGBA8888 and nothing changed.

SDL Create window not creating window on Mac os X

I am following the Lazy Foo tutorial on SDL.
When creating a window with SDL_WINDOW_SHOWN and running the binary the menu bar appears, but the window does not. Need some direction on what I can do to further debug this.
I've also tried to manually set window position with no luck.
SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
SDL_CreateWindow("SDL Tutorial", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
Everything compiles successfully and SDL_GetError() does not produce any errors. I logged to a file to confirm.
#include <SDL2/SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main(int argc, const char * argv[]) {
FILE *log_file;
log_file = fopen("log.log", "a+");
//The window we'll be rendering to
SDL_Window* window = NULL;
//The surface contained by the window
SDL_Surface* screenSurface = NULL;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(log_file, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
}
else
{
//Create window
window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL)
{
fprintf(log_file, "Window could not be created! SDL_Error: %s\n", SDL_GetError());
}
else
{
//Get window surface
screenSurface = SDL_GetWindowSurface(window);
//Fill the surface white
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
//Update the surface
SDL_UpdateWindowSurface(window);
fprintf(log_file, "SDL_Error: %s\n", SDL_GetError());
//Wait two seconds
SDL_Delay(2000);
}
}
//Destroy window
SDL_DestroyWindow(window);
//Quit SDL subsystems
SDL_Quit();
return 0;
}
The reason it was not working was because I did not have an event loop set up.
I the documentation on how to draw a line and got an example there.
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
done = SDL_TRUE;
}
}

C SDL VS Code image is not displaying

I have recently started learning game programming in C. I am using a virtual studio code on linux Ubuntu, and image star.png is not displaying on the screen, there are no errors or problems reported by vs code. I have tried reinstalling sdl2-dev and sdl2-image-dev libraries. Maybe it is a problem with SDL, or maybe with my code (it was written in 2013).
The code should draw a white ractangl, which i can move around, on a blue screen and place a star in upper left corner of a window. It does everything except placing a star.
The code: (doesn't show "Cannot dinf star.png!" so I guess it is not that)
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
typedef struct
{
int x, y;
short life;
char *name;
}Man;
typedef struct
{
//players
Man man;
//image
SDL_Texture *star;
} GameState;
/////////////////////////////////////////////////////////////////////processEvents
int processEvents(SDL_Window *window , GameState *game)
{
SDL_Event event;
int done = 0;
//Check for events
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_WINDOWEVENT_CLOSE:
{
if (window)
{
SDL_DestroyWindow(window);
window = NULL;
done = 1;
}
}
break;
case SDL_KEYDOWN:
{
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
done = 1;
break;
}
}
break;
case SDL_QUIT:
done = 1;
break;
}
}
const Uint8 *state = SDL_GetKeyboardState(NULL);
if(state[SDL_SCANCODE_LEFT])
{
game->man.x -= 1;
}
if(state[SDL_SCANCODE_RIGHT])
{
game->man.x += 1;
}
if(state[SDL_SCANCODE_UP])
{
game->man.y -= 1;
}
if(state[SDL_SCANCODE_DOWN])
{
game->man.y += 1;
}
SDL_Delay(10);
return done;
}
/////////////////////////////////////////////////////////////////////////////////////doRender
void doRedner(SDL_Renderer *renderer, GameState *game)
{
SDL_SetRenderDrawColor(renderer , 0, 0, 255, 255); //blue
//screen clean up into blue
SDL_RenderClear(renderer);
//set color
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect rect = {game->man.x, game->man.y, 80, 80};
SDL_RenderFillRect(renderer, &rect);
//draw a star
SDL_Rect starRect = { 50, 50, 64, 64 };
SDL_RenderCopy(renderer, game->star, NULL, &starRect);
SDL_RenderPresent(renderer);
}
/////////////////////////////////////////////////////////////////////////////////////main funkction
int main(int argc, char *argv[])
{
GameState gameState;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Surface *starSurface = NULL;
SDL_Init(SDL_INIT_VIDEO);
gameState.man.x = 340-40;
gameState.man.y = 240-40;
window = SDL_CreateWindow("Game Widnow",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
0
);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
//load image
starSurface = IMG_Load("star.png");
if(starSurface = NULL)
{
printf("Cannot find star.png!\n\n");
SDL_Quit();
return 1;
}
gameState.star = SDL_CreateTextureFromSurface(renderer, starSurface);
SDL_FreeSurface(starSurface);
int done = 0;
while(!done)
{
//check events
done = processEvents(window, &gameState);
//render
doRedner(renderer, &gameState);
}
SDL_Delay(1000);
//exit game and free memory
SDL_DestroyTexture(gameState.star);
//destroy and close window
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
return 0;
}
TL;DR: Your intention is to perform a comparison, but you are using the assignment operator (i.e., =) instead of the comparison operator ==.
The conditional expression in your if statement:
if(starSurface = NULL)
{
...
}
does not check whether starSurface is NULL. The expression starSurface = NULL assigns NULL to starSurface and evaluates to NULL. Therefore, the condition evaluates to false and no error message is displayed.
Instead, you should write (note the double = below):
if (starSurface == NULL)
or just:
if (!starSurface)

C SDL2 - Selecting part of an image and then zooming in

So, I have an image that I open in a window with SDL2. I want to implement a zoom function for the image that works as following: I click on two points of the image and I make a rectangle with those points as opposite corners. Then I make a copy of the rectangle and update the window to show the part of the image I selected, and this new image has a bigger width and height than the rectangle I chose because it will be the height and the width of the whole window.
I can detect the mouse clicks, and from the mouse clicks calculate the top left corner of the rectangle in x and y coordinates. However, I don't know how to make a copy of the pixels in those rectangles, nor how to make the window now show the zoomed in part. I've been googling a lot but I don't know what functions to use or how to code my own. How would I write such a function?
Here's what I have so far. The image I want to be able to zoom into is "map.jpg"
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = NULL;
window = SDL_CreateWindow("WarmingUp", TOP_LEFT_CORNER_X,
TOP_LEFT_CORNER_Y, IMAGE_WIDTH, IMAGE_HEIGHT, 0);
if(window == NULL){
printf("Erro a abrir janela gráfica\n");
exit(EXIT_FAILURE);
}
SDL_Renderer *renderer = NULL;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL){
printf("Erro a criar renderer\n");
exit(EXIT_FAILURE);
}
SDL_Surface *jpgSurface = NULL;
jpgSurface = IMG_Load("map.jpg");
if(jpgSurface == NULL){
printf("Erro a abrir imagem\n");
exit(EXIT_FAILURE);
}
SDL_Texture *jpgTexture = NULL;
jpgTexture = SDL_CreateTextureFromSurface(renderer, jpgSurface);
if(jpgTexture == NULL){
printf("Erro a criar superfície através de imagem\n");
exit(EXIT_FAILURE);
}
SDL_FreeSurface(jpgSurface);
SDL_Event e;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, jpgTexture, NULL, NULL);
while(!quit){
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT)
quit = 1;
SDL_RenderPresent(renderer);
SDL_Delay(15);
SDL_DestroyTexture(jpgTexture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
I know that to make the mouse interaction in my while &eventloop I need to check for if ( event.type == SDL_MOUSEBUTTONUP ), and I can also show you the code to calculate the top left corner x and y position of the desired rectangle to be cropped, but I really can't go any further.
Here is modified code from question:
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#define IMAGE_WIDTH 1024
#define IMAGE_HEIGHT 768
int main(int argc, char **argv) {
(void)argc, (void)argv;
int quit = 0;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = NULL;
window = SDL_CreateWindow("WarmingUp",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
IMAGE_WIDTH, IMAGE_HEIGHT, 0);
if(window == NULL){
printf("Erro a abrir janela gráfica\n");
exit(EXIT_FAILURE);
}
SDL_Renderer *renderer = NULL;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL){
printf("Erro a criar renderer\n");
exit(EXIT_FAILURE);
}
SDL_Surface *jpgSurface = NULL;
jpgSurface = IMG_Load("map.jpg");
if(jpgSurface == NULL){
printf("Erro a abrir imagem\n");
exit(EXIT_FAILURE);
}
SDL_Texture *jpgTexture = NULL;
jpgTexture = SDL_CreateTextureFromSurface(renderer, jpgSurface);
if(jpgTexture == NULL){
printf("Erro a criar superfície através de imagem\n");
exit(EXIT_FAILURE);
}
SDL_FreeSurface(jpgSurface);
SDL_Event e;
// rectangle to upscale in second window
const SDL_Rect srcrect = {600, 500, 250, 250};
SDL_Window *second_window = NULL;
SDL_Renderer *second_renderer = NULL;
SDL_Texture *magnified_fragment_texture = NULL;
while(!quit){
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT ||
(e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE)) {
quit = 1;
} else if(e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_t &&
!second_window) {
// create empty surface of adequate size
SDL_Surface *const surf = SDL_CreateRGBSurface(0, srcrect.w, srcrect.h, 32,
0xff000000, 0xff0000, 0xff00, 0xff);
SDL_FillRect(surf, NULL, 0);
// copy pixels
SDL_RenderReadPixels(renderer, &srcrect, SDL_PIXELFORMAT_RGBA8888,
surf->pixels, surf->pitch);
// error checking should be done...
second_window = SDL_CreateWindow("mag",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
srcrect.w*2, srcrect.h*2, 0);
second_renderer = SDL_CreateRenderer(second_window, -1, SDL_RENDERER_ACCELERATED);
magnified_fragment_texture = SDL_CreateTextureFromSurface(second_renderer, surf);
SDL_FreeSurface(surf);
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, jpgTexture, NULL, NULL);
SDL_RenderPresent(renderer);
if(second_renderer) {
const SDL_Rect dstrect = {0, 0, srcrect.w*2, srcrect.h*2};
SDL_RenderClear(second_renderer);
// RenderCopy scales texture to destination rect
SDL_RenderCopy(second_renderer, magnified_fragment_texture, NULL, &dstrect);
SDL_RenderPresent(second_renderer);
}
SDL_Delay(15);
}
SDL_DestroyTexture(jpgTexture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
In keypress event handler it creates second window/renderer/texture (not the best place for it), and later it renders second window if it exists.

Displaying the rectangle using a for loop in SDL2 C

I'm currently using a for loop to create 4 rectangles along the x-axis. When I run the program the image of the rectangle appears for a few millisecond before it disappears. Can you show me what I have to do in order for the image to display on the screen permanently.
When I create individual rectangles it display's the image without vanishing.
An example will be helpful. Thanks
int main(){
int barrack1_xposition = 167,i=1;
int cnt_barrack=0;
SDL_Window *o = NULL;
SDL_Renderer *r = NULL;
SDL_Rect bar1[4];
SDL_Event e;
SDL_Init(SDL_INIT_VIDEO);
o = SDL_CreateWindow("SPACE INVADERS",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
1024,
800,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
r = SDL_CreateRenderer(o, -1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
while(i)
{
while(SDL_PollEvent(&e) !=0)
{
if(e.type == SDL_QUIT)
i=0;
}
for(cnt_barrack = 0; cnt_barrack < 4; cnt_barrack++)
{
bar1[cnt_barrack].x=barrack1_xposition;
bar1[cnt_barrack].y=250;
bar1[cnt_barrack].h=50;
bar1[cnt_barrack].w=50;
barrack1_xposition += 200;
}
SDL_SetRenderDrawColor(r,255,255,255,255);
SDL_RenderFillRect(r,&bar1[0]);
SDL_RenderFillRect(r,&bar1[1]);
SDL_RenderFillRect(r,&bar1[2]);
SDL_RenderFillRect(r,&bar1[3]);
SDL_RenderPresent(r);
}
SDL_DestroyWindow(o);
SDL_DestroyRenderer(r);;
SDL_Quit();
}
Your problem caused by the fact you didn't reset barrack1_xposition on each frame, so it keeps going higher and higher. I initially didn't notice that because there was no RenderClear so it seemed to be fine but actually wasn't.
#include "SDL.h"
int main(){
int barrack1_xposition,i=1;
int cnt_barrack=0;
SDL_Window *o = NULL;
SDL_Renderer *r = NULL;
SDL_Rect bar1[4];
SDL_Event e;
SDL_Init(SDL_INIT_VIDEO);
o = SDL_CreateWindow("SPACE INVADERS",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
1024,
800,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
r = SDL_CreateRenderer(o, -1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
while(i)
{
while(SDL_PollEvent(&e) !=0)
{
if(e.type == SDL_QUIT)
i=0;
}
barrack1_xposition=167;
for(cnt_barrack = 0; cnt_barrack < 4; cnt_barrack++)
{
bar1[cnt_barrack].x=barrack1_xposition;
bar1[cnt_barrack].y=250;
bar1[cnt_barrack].h=50;
bar1[cnt_barrack].w=50;
barrack1_xposition += 200;
}
SDL_SetRenderDrawColor(r,0,0,0,0);
SDL_RenderClear(r);
SDL_SetRenderDrawColor(r,255,255,255,255);
SDL_RenderFillRect(r,&bar1[0]);
SDL_RenderFillRect(r,&bar1[1]);
SDL_RenderFillRect(r,&bar1[2]);
SDL_RenderFillRect(r,&bar1[3]);
SDL_RenderPresent(r);
}
SDL_DestroyWindow(o);
SDL_DestroyRenderer(r);;
SDL_Quit();
return 0;
}

Resources