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

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.

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)

How do I enable SDL2 to allow all key_down events and not just the quit event?

My SDL Window displays a bitmap on top of another bitmap however I am not able to control the bitmap image using SDL_KeyDown events. The only event that works is SDL_QUIT.
I attempted to map using the ASWD keys and the Up Down Left and Right arrow keys, however the SDL Window doesn't process any of those events. The only event that occurs is when I click the X button to close the window or when I press the q or escape keys to close the window. Also, none of the printf statements are appearing on the console so I am not sure exactly where the problem is because the program compiles and runs without any errors.
I am using C not C++
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_timer.h>
#include <SDL_image.h>
#define WINDOW_WIDTH (640)
#define WINDOW_HEIGHT (480)
// Speed in Pixels/Seconds
#define SCROLL_SPEED (300)
// Spirte Size
#define SPRITE_SIZE 32
int main(int argc, const char * argv[]) {
// Attempt to initialize graphics library
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return 1;
}
//Initialize Key Variables
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Surface *windowSurface = NULL;
SDL_Surface *imageSurface = NULL;
// Creating Surfaces and Addresses
SDL_Surface *temp = NULL;
SDL_Surface *sprite = NULL;
// set sprite position
SDL_Rect dstSprite = {0, 0, 32, 32};
// Attempt to create SDL Window
window = SDL_CreateWindow("Atari Lunar Lander", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
// Print an Error if the window fails to initialize
if (window == NULL) {
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
// Create The Title of the Window
SDL_SetWindowTitle(window, "Atari Lunar Lander");
// Create SDL Render Window
Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE;
renderer = SDL_CreateRenderer(window, -1, render_flags);
// if rendering the window fails
if (!renderer) {
printf("error creating render: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
windowSurface = SDL_GetWindowSurface(window);
// Load A bitmap image as an Surface
windowSurface = SDL_LoadBMP("/Users/jeremiahonwubuya/Desktop/AtariLunarLander/outerspace.bmp");
if (!windowSurface) {
printf("error creating surface: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
//Load Image data into graphics hardware memory
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, windowSurface);
if (!texture) {
printf("error creating texture: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// load bitmap image into an SDL_Surface "temp" and store "temp" into the SDL_Surface "sprite" that is optimized for Blitting
temp = SDL_LoadBMP("/Users/jeremiahonwubuya/Desktop/AtariLunarLander/player.bmp");
sprite = SDL_ConvertSurface(temp, temp->format, 0);
SDL_FreeSurface(temp);
SDL_Texture *playerTexture = SDL_CreateTextureFromSurface(renderer, sprite);
if (!sprite) {
printf("error creating sprite: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// updating the Window Surface to be the BitMap Image
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderCopy(renderer, playerTexture, NULL, &dstSprite);
SDL_RenderPresent(renderer);
bool isRunning = true;
SDL_Event event;
while (isRunning) {
while (SDL_PollEvent(&event) != 0 ) {
if (event.type == SDL_QUIT) {
// close button clicked
isRunning = false;
break;
}
if (event.type == SDL_KEYDOWN) {
// close program upon buttons pressed
switch(event.key.keysym.sym){
case SDLK_q:
printf("program stopped");
isRunning = false;
break;
case SDLK_ESCAPE:
printf("programm stopped");
isRunning = false;
break;
}
// Animating the Sprite
if (event.key.keysym.sym == SDLK_a || event.key.keysym.sym == SDLK_LEFT) {
dstSprite.x -= 5;
dstSprite.w = 32;
dstSprite.h = 32;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderCopy(renderer, playerTexture, NULL, &dstSprite);
SDL_RenderPresent(renderer);
printf("sprite moved leftt to position %d \n", dstSprite.x);
}
if (event.key.keysym.sym == SDLK_d || event.key.keysym.sym == SDLK_RIGHT) {
dstSprite.x += 5;
dstSprite.w = 32;
dstSprite.h = 32;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderCopy(renderer, playerTexture, NULL, &dstSprite);
SDL_RenderPresent(renderer);
printf("sprite moved right to position %d \n", dstSprite.x);
}
if (event.key.keysym.sym == SDLK_w || event.key.keysym.sym == SDLK_UP) {
dstSprite.y -= 5;
dstSprite.w = 32;
dstSprite.h = 32;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderCopy(renderer, playerTexture, NULL, &dstSprite);
SDL_RenderPresent(renderer);
printf("sprite moved up to position %d \n", dstSprite.y);
}
if (event.key.keysym.sym == SDLK_s || event.key.keysym.sym == SDLK_DOWN) {
dstSprite.y += 5;
dstSprite.w = 32;
dstSprite.h = 32;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderCopy(renderer, playerTexture, NULL, &dstSprite);
SDL_RenderPresent(renderer);
printf("sprite moved down to position %d \n", dstSprite.y);
}
}
// Prevent Sprite from colliding with the edges of the screen
if ( dstSprite.x < 0 ) {
dstSprite.x = 0;
}
else if ( dstSprite.x > WINDOW_WIDTH-SPRITE_SIZE ) {
dstSprite.x = WINDOW_WIDTH-SPRITE_SIZE;
}
if ( dstSprite.y < 0 ) {
dstSprite.y = 0;
}
else if ( dstSprite.y > WINDOW_HEIGHT-SPRITE_SIZE ) {
dstSprite.y = WINDOW_HEIGHT-SPRITE_SIZE;
}
// Draw the Sprite
SDL_BlitSurface(sprite, NULL, windowSurface, &dstSprite);
SDL_UpdateWindowSurface(window);
}
}
SDL_Delay(5000);
// Clean Up
// Free SDL Surfaces
SDL_FreeSurface(sprite);
sprite = NULL;
SDL_FreeSurface(windowSurface);
windowSurface = NULL;
SDL_FreeSurface(imageSurface);
imageSurface = NULL;
// Destroy Texture and Window
texture = NULL;
SDL_DestroyTexture(texture);
playerTexture = NULL;
SDL_DestroyTexture(playerTexture);
window = NULL;
SDL_DestroyWindow(window);
SDL_Quit();
}
Your OR'ing your cases thus producing different integers than what are in .sym. You should split your ORs and let them fallthrough or use an if condition. For example:
case SDLK_a:
case SDLK_LEFT:
printf("sprite moved leftt");
break;
case SDLK_d:
case SDLK_RIGHT:
printf("sprite moved right");
break;
case SDLK_w:
case SDLK_UP:
printf("sprite moved up");
break;
case SDLK_s:
case SDLK_DOWN:
printf("sprite moved down");
break;
Or
if (event.key.keysym.sym == SDLK_a || event.key.keysym.sym == SDLK_LEFT)
// do something..
So a few things, first your code is "working", I get printed the text out when I press the correct keys but you have another error, those breaks in the ifs are making you skip the bliting section, remember that breaks break the execution flow in switch, for, and while loops, you don't need them on ifs.
Also If you press a key and then don't press anything you should be getting your screen updated. I tested with this program. Tell me if it is receiving your keys..
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
SDL_Init(SDL_INIT_VIDEO);
bool isRunning = true;
SDL_Event event;
SDL_Rect dstSprite = {0, 0, 0, 0};
while (isRunning) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
// close button clicked
isRunning = false;
break;
}
if (event.type == SDL_KEYDOWN) {
// close program upon buttons pressed
switch (event.key.keysym.sym) {
case SDLK_q:
printf("program stopped");
isRunning = false;
break;
case SDLK_ESCAPE:
printf("programm stopped");
isRunning = false;
break;
}
// Animating the Sprite
if (event.key.keysym.sym == SDLK_a ||
event.key.keysym.sym == SDLK_LEFT) {
dstSprite.x -= 5;
printf("sprite moved leftt to position %d \n", dstSprite.x);
break;
}
if (event.key.keysym.sym == SDLK_d ||
event.key.keysym.sym == SDLK_RIGHT) {
dstSprite.x += 5;
printf("sprite moved right to position %d \n", dstSprite.x);
break;
}
if (event.key.keysym.sym == SDLK_w ||
event.key.keysym.sym == SDLK_UP) {
dstSprite.y -= 5;
printf("sprite moved up to position %d \n", dstSprite.y);
break;
}
if (event.key.keysym.sym == SDLK_s ||
event.key.keysym.sym == SDLK_DOWN) {
dstSprite.y += 5;
printf("sprite moved down to position %d \n", dstSprite.y);
break;
}
}
printf("update graphics\n");
}
}
}

Retina Display Support with SDL

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.

Resources