Image doesn't load to window - c++

Solution:
Works when writing the whole image adress instead of only the name
I know I asked a similar question recently but I haven't got a solution yet. I'm trying to load a simple bmp picture to a window in c++ and SDL. This is my code:
int main(int argc, char *argv[])
{
//init
SDL_Init( SDL_INIT_EVERYTHING );
//screen window
SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 32, SDL_SWSURFACE);
SDL_WM_SetCaption( WINDOW_TITLE, 0 );
//image
SDL_Surface *background = IMG_Load("images.png");
SDL_Event event;
bool gameRunning = true;
//loop
while (gameRunning)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gameRunning = false;
}
}
SDL_BlitSurface(background, NULL, screen, NULL);
//update screen
SDL_Flip(screen);
}
//quit
SDL_Quit();
return 0;
}
Is there anything wrong with my code or can be a problem with my computer? I know I have placed the image in the project folder and spelt it the right way.

Related

SDL2 SDL_GetRenderDrawColor only able to display black

I am able to make a window with SDL2 and C++ that has a black background, as soon as I change one of the arguments from SDL_GetRenderDrawColor from 0 to anything else I get this error:
Error (active) E0167 argument of type "int" is incompatible with
parameter of type "Uint8 *"
this is my code:
#include <stdio.h>
#include <SDL.h>
#undef main
int main(int argc, char** argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window * window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
bool quit = false;
while (!quit) {
SDL_Delay(10);
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT: quit = true; break;
}
SDL_GetRenderDrawColor(renderer, 255, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I am working with Visual Studio 2019 Community, I already succesfully set up SDL2 in Visual Studio at my Workplace but doing the same things at home yielded this error.
If you want to set the render draw color, you should use SDL_SetRenderDrawColor. SDL_GetRenderDrawColor is used to get the render draw color.

SDL_FINGERDOWN not registering correctly

I don't why SDL_FINGERDOWN records
even simple touch event. It should register on Sliding finger down as name says, right? I use android N for testing.
Here is my code
#include "SDL.h"
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
int r, g, b;
r = g = b = 0;
SDL_Window *window;
window = SDL_CreateWindow("S", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 720, 1320, SDL_WINDOW_RESIZABLE);
SDL_Surface *screen = SDL_GetWindowSurface(window);
Uint32 white = SDL_MapRGB(screen->format, 255, 255, 255);
SDL_FillRect(screen, NULL, white);
SDL_UpdateWindowSurface(window);
SDL_Event event;
bool running = true;
while (running)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_FINGERDOWN)
{
running = false;
break;
}
//end if
} //end inner while
}
SDL_DestroyWindow(window);
SDL_Quit();
}
You have misunderstood meaning of SDL_FINGERDOWN and SDL_FINGERUP.
When you land your finger on touch screen then that's event of type SDL_FINGERDOWN.When you lift up your finger from touch screen then that's event of type SDL_FINGERUP.

From SMFL to SDL 2.0

Here is some code in SMFL
RenderWindow window(VideoMode(320, 480), "The Game!");
Texture t1,t2,t3;
t1.loadFromFile("images/tiles.png");
t2.loadFromFile("images/background.png");
t3.loadFromFile("images/frame.png");
Sprite s(t1), background(t2), frame(t3);
Does SDL 2.0 has functions like this and how to convert them to SDL 2.0
yes, all is there:
https://programmersranch.blogspot.kr/2014/03/sdl2-animations-with-sprite-sheets.html
#include <SDL.h>
#include <SDL_image.h>
int main(int argc, char ** argv)
{
bool quit = false;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
SDL_Window * window = SDL_CreateWindow("SDL2 Sprite Sheets",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,
480, 0);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface * image = IMG_Load("spritesheet.png");
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer,
image);
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
}
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
SDL_FreeSurface(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 0;
}

SDL_Renderer won't show textures after passing through the function

I have a little problem with SDL_Renderer. I can't understand why it doesn't work. Let's look at this example, it works fine:
bool running = true;
SDL_Window* window = SDL_CreateWindow("ASDF", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Texture* texture = IMG_LoadTexture(renderer, "asdf.bmp");
SDL_Event event;
while(running)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE)
{
running = false;
}
break;
default:
break;
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
Then it comes to classes and when I pass the renderer through functions, it won't work anymore.
class Sprite
{
public:
Sprite(const std::string& path) : filePath(path) {};
~Sprite() { SDL_DestroyTexture(tex); };
void draw(SDL_Renderer* renderer);
private:
const std::string& filePath;
SDL_Texture* tex;
};
void Sprite::draw(SDL_Renderer* renderer)
{
printf("renderer sprite = %p\n", renderer);
tex = IMG_LoadTexture(renderer, filePath.c_str());
SDL_RenderCopy(renderer, tex, NULL, NULL);
}
int main(int argc, char **argv)
{
bool running = true;
SDL_Window* window = SDL_CreateWindow("ASDF", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event event;
Sprite* sprite = new Sprite("asdf.bmp");
while(running)
{
printf("renderer main = %p\n", renderer);
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE)
{
running = false;
}
break;
default:
break;
}
}
SDL_RenderClear(renderer);
sprite->draw(renderer);
SDL_RenderPresent(renderer);
}
return 0;
}
The address of renderer is the same in main and in the draw function. I know that I'm probably making some sort of beginner's mistake here but i can't find it out.
The texture is being loaded every frame when you call sprite->draw.
You should move the line tex = IMG_LoadTexture(renderer, filePath.c_str()); to the constructor so that it is only loaded once.
The reason for this is that the texture will not be ready for rendering in the same frame that it is loaded.
As a side note you don't appear to be cleaning up the SDL_Window or SDL_Renderer with their respective destroy functions or calling SDL_Quit although I accept this may have been omitted for submitting the code example.

Problems with SDL_SetColorKey

I'm trying to create a transparent sprite with SDL. I'm using SDL_SetColorKey on a bitmap with magenta (0xff00ff) background (it's 100% magenta, I checked it with the GIMP :)) The call to SDL_SetColorKey looks like this:
SDL_SetColorKey( bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255) );
The call to SDL_SetColorKey apparently returns 0, however there is no transparency. Can anyone tell me what I am missing here?
Here is the problematic code in case anyone wants to test it:
#include "SDL/SDL.h"
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = "SDL Start";
int main(int argc, char **argv)
{
SDL_Init( SDL_INIT_VIDEO );
SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCaption( WINDOW_TITLE, 0 );
SDL_Surface* bitmap = SDL_LoadBMP("resources/ship.bmp");
if(SDL_SetColorKey( bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255) )) printf("aaaaa %s", SDL_GetError());
// Part of the screen we want to draw the sprite to
SDL_Rect destination;
destination.x = 100;
destination.y = 100;
destination.w = 65;
destination.h = 44;
SDL_Event event;
bool gameRunning = true;
while (gameRunning)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gameRunning = false;
}
}
SDL_BlitSurface(bitmap, NULL, screen, &destination);
SDL_Flip(screen);
}
SDL_FreeSurface(bitmap);
SDL_Quit();
return 0;
}
UPDATE:
In case anyone needs it, here is the bitmap: http://dl.dropbox.com/u/8936880/ship.bmp
The problem is with your image, i used one generated by me and it works out of the box with your code.
Your image is in 32 bits and it seems that SDL_SetColorKey doesn't like it, convert it to 24 bits and it should work.
You can convert it with Gimp when you save it to BMP from the advanced settings.
Try with this one converted to 24 bits.