SDL: Blitting BMP to window surface black screen mystery - c++

I've written the following code to load a BMP image as a surface and then blit that image onto the window:
#include "stdafx.h"
#include "SDL.h"
#include <iostream>
int main(int argc, char *argv[])
{
//init
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("Playground", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500, 500, 0);
std::cout << SDL_GetError() << std::endl;
SDL_Surface* surface = SDL_GetWindowSurface(window);
//load file and convert to texture
SDL_Surface* bmp = SDL_LoadBMP("sample.bmp");
std::cout << SDL_GetError() << std::endl;
//render texture
SDL_Rect area;
area.x, area.y = 3;
area.h, area.w = 25;
SDL_BlitSurface(bmp, &area, surface, &area);
std::cout << SDL_GetError() << std::endl;
SDL_UpdateWindowSurface(window);
std::cout << SDL_GetError() << std::endl;
SDL_Delay(3000);
//clean up
SDL_FreeSurface(bmp);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
When I press F5 (I'm working in Visual Studio Express 2017) to build and run the program, the program created runs, creates a window, and then the window remains entirely black as the program runs. I receive no error messages from V.S., SDL_GetError(), or Windows.
There appears to be no problems but the image just gets lost somewhere, it seems. Would anyone be able to help me?
P.S. Here is the bmp I am trying to display:

This code doesn't do what you think it does:
area.x, area.y = 3;
area.h, area.w = 25;
You should change it to
area.x = area.y = 3;
area.h = area.w = 25;
to have multiple assignments. Or even better just initialize SDL_Rect inline:
SDL_Rect area = { 3, 3, 25, 25 };

Related

Why doesn't my SDL2 code display images with SDL_Texture*

I have a C++ project where I'm initially trying to display a PNG image to the screen.
This is my code.
RenderWindow.hpp
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
class RenderWindow
{
public:
RenderWindow(const char *p_title, int p_width, int p_height);
void render();
void cleanUp();
private:
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Surface *image = IMG_Load("~/SDL2_Game/images/Green_Tile.png");
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, image);
};
RenderWindow.cpp
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include "RenderWindow.hpp"
RenderWindow::RenderWindow(const char* p_title, int p_w, int p_h):window(NULL), renderer(NULL)
{
window = SDL_CreateWindow(p_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, p_w, p_h, SDL_WINDOW_SHOWN);
if (window == NULL) std::cout << "Window failed to init: " << SDL_GetError() << std::endl;
renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
}
void RenderWindow::render(){
SDL_RenderClear(renderer);
//SDL_Rect dstrect = { 5, 5, 320, 240 };
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
void RenderWindow::cleanUp(){
SDL_DestroyTexture(texture);
SDL_FreeSurface(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
main.cpp
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include "RenderWindow.hpp"
int main(int argc, char** argv){
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cout << "Problem with initialization. " << SDL_GetError() << std::endl;
}
else {
std::cout << "Initialization success!" <<std::endl;
}
if (!IMG_Init(IMG_INIT_PNG)){
std::cout << "Problem with Image initialization " <<SDL_GetError() << std::endl;
}
RenderWindow win("RPG_Game_v_1.0", 800, 600);
win.render();
bool gameRunning = true;
SDL_Event event;
while(gameRunning){
while(SDL_PollEvent(&event)){
if (event.type == SDL_QUIT) gameRunning = false;
}
}
win.cleanUp();
IMG_Quit();
SDL_Quit();
return 0;
}
I'm on a Linux machine.
I compile this with
g++ -g -o game ./*.cpp -lSDL2main -lSDL2 -lSDL2_image
Only a window is displaying. There is no image. I've tried refactoring my code with SDL_BlitSurface() and it does indeed display the PNG image. But why is this code not working? is it due to the fact that I'm using SDL_Texture* and my current system does not have a discrete graphics card?
I think that the call to SDL_CreateTextureFromSurface fails because it is called before SDL_CreateWindow and SDL_CreateRenderer, thereby initializing texture to NULL.
Please move the initizalization of texture (and image) to after window and renderer are initialized.
To further help with such issues, please check if the result of SDL functions != NULL and print SDL_GetError() to get more information about what went wrong.

SDL_DestroyWindow doesn't free memory

Basically, in my program I have to create and destroy a window. I always use the same window pointer creating and destroying the contents (texture and render).
I notice a memory leak in this process so I wrote a simple program to verify this, here the code.
The problem has the higher impact on Windows, on Ubuntu at the end of the test the memory is increased only around 3MB (around 236MB on Windows).
#include <iostream>
#include "SDL.h"
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* mainWindow;
SDL_Renderer* mainRenderer;
SDL_Texture* mainTexture;
for (int i = 0; i < 200; i++)
{
mainWindow = SDL_CreateWindow("Memory Leak Test v1.0", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, SDL_WINDOW_SHOWN);
mainRenderer = SDL_CreateRenderer(mainWindow, -1, SDL_RENDERER_ACCELERATED);
mainTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, 320, 240);
if (i == 0)
{
std::cout << "SDL Memory Leak Tester, please check the amount of memory\noccupied by the process, then press Enter\n";
getchar();
}
SDL_Delay(50);
if (i == 199)
{
std::cout << "SDL Memory Leak Tester, please check the amount of memory\noccupied by the process, then press Enter\n";
getchar();
}
SDL_DestroyTexture(mainTexture);
SDL_DestroyRenderer(mainRenderer);
SDL_DestroyWindow(mainWindow);
}
std::cout << "Test finished, press Enter to close\n";
getchar();
return 0;
}
Any idea?

SDL2 empty transparent window in Linux

Here is some sample SDL2 code I tried to run on my Linux computer running Ubuntu 18.04 with KDE Plasma Desktop Environment (I have multiple desktop environments installed in case it is relevant):
#include<iostream>
#include<SDL2/SDL.h>
int main(int argc, char** argv)
{
if(SDL_Init(SDL_INIT_VIDEO) != 0){
std::cerr << "SDL_Init() Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Window* win = SDL_CreateWindow(
"Hello world",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640,480,
0
);
if(win == nullptr){
std::cerr << "SDL_CreateWindow() Error: " << SDL_GetError() << std::endl;
return 1;
}
//Create and init the renderer
SDL_Renderer* ren = SDL_CreateRenderer(win, -1, 0);
if(ren == nullptr){
std::cerr << "SDL_CreateRenderer() Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(win);
return 1;
}
//Render something
SDL_RenderSetLogicalSize(ren,640,480);
//Set colour of renderer
SDL_SetRenderDrawColor(ren,255,0,0,255);
//Clear the screen to the set colour
SDL_RenderClear(ren);
//Show all the has been done behind the scenes
SDL_RenderPresent(ren);
//Delay so that we can see what is on the screen
SDL_Delay(5000);
//Clean Up
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
The red window that is supposed to appear appears only once when I run the program for the first time. All subsequent executions produce an empty transparent window with whatever is in the background. The background image drags along with the window.
I have tried SDL_WINDOW_SHOWN flag in SDL_CreateWindow() as well as SDL_RENDER_ACCELERATED flag for SDL_CreateRenderer().
The only way to produce the red screen again is to reboot the system.
I even compiled and ran this with an IDE (CodeLite) and I still got the same results.
This particular question on SO shows similar problems. But the OP isn't using Linux and the problem isn't exactly the same.
Other posts on this website mention event processing but I haven't gotten that far. If at all it is necessary, I would be grateful for some resources on it as the documentation doesn't explain much.
Update:This program runs fine on another computer running Lubuntu 18.10.
Replace the SDL_Delay() (which blocks all event processing like notifying X11/Wayland & your window manager that your process is still alive) with a loop that calls SDL_PumpEvents() somehow, either directly (like below) or indirectly via SDL_PollEvent()/SDL_WaitEvent():
const Uint32 startMs = SDL_GetTicks();
while( SDL_GetTicks() - startMs < 5000 )
{
SDL_PumpEvents();
//Render something
SDL_RenderSetLogicalSize(ren,640,480);
//Set colour of renderer
SDL_SetRenderDrawColor(ren,255,0,0,255);
//Clear the screen to the set colour
SDL_RenderClear(ren);
//Show all the has been done behind the scenes
SDL_RenderPresent(ren);
}
All together:
#include <iostream>
#include <SDL2/SDL.h>
int main( int argc, char** argv )
{
if( SDL_Init( SDL_INIT_VIDEO ) != 0 )
{
std::cerr << "SDL_Init() Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Window* win = SDL_CreateWindow
(
"Hello world",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640, 480,
0
);
if( win == nullptr )
{
std::cerr << "SDL_CreateWindow() Error: " << SDL_GetError() << std::endl;
return 1;
}
//Create and init the renderer
SDL_Renderer* ren = SDL_CreateRenderer( win, -1, 0 );
if( ren == nullptr )
{
std::cerr << "SDL_CreateRenderer() Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow( win );
return 1;
}
const Uint32 startMs = SDL_GetTicks();
while( SDL_GetTicks() - startMs < 5000 )
{
SDL_PumpEvents();
//Render something
SDL_RenderSetLogicalSize( ren, 640, 480 );
//Set colour of renderer
SDL_SetRenderDrawColor( ren, 255, 0, 0, 255 );
//Clear the screen to the set colour
SDL_RenderClear( ren );
//Show all the has been done behind the scenes
SDL_RenderPresent( ren );
}
//Clean Up
SDL_DestroyRenderer( ren );
SDL_DestroyWindow( win );
SDL_Quit();
return 0;
}

SDL_LoadBMP() displaying a black surface while surface is referenced

Trying to load an image onto an SDL_Surface. However the surface is always black although the bmp is clearly not.
NOTE
This is a working SDL_Window and creating a surface pointer is successful, what is unsuccessful is loading "Kassadin.bmp" which is located in the Code::Blocks project folder. It displays a black surface.
ALL answers on this particular question have NOT solved this problem, before marking this as a duplicate.
#include <iostream>
#include <SDL.h>
#include <stdio.h>
using namespace std;
const int SCREEN_WIDTH = 700;
const int SCREEN_HEIGHT = SCREEN_WIDTH / 12 * 9;
//Create an SDL_Window pointer
SDL_Window* window = NULL;
//Create an SDL_Surface pointer
SDL_Surface* surface = NULL;
//SDL_Surface for an image
SDL_Surface* imgSurface = NULL;
bool init(){
//try init SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0){
cout << "Failed init SDL" << endl;
return false;
}else{
//create window title x pos y pos width height flags
//This doesnt include a surface ie it will be a plain window
window = SDL_CreateWindow("An SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
}
if(window == NULL){
cout << "Failed creating SDL window" << endl;
return false;
}else{
// creates surface with an SDL_Window object
surface = SDL_GetWindowSurface(window);
}
return true;
}
bool loadMedia(){
imgSurface = SDL_LoadBMP("Kassadin.bmp");
return true;
}
void close(){
//Sets the SDL_Window pointer to NULL again
SDL_DestroyWindow(window);
window = NULL;
SDL_FreeSurface (imgSurface);
imgSurface = NULL;
//quits
SDL_Quit();
}
int main(int argc, char* argv[]){
if(!init()){
cout << "error init sdl" << endl;
}else{
if(!loadMedia){
cout << "Trouble loading media..." << endl;
}else{
SDL_BlitSurface(imgSurface, NULL, surface, NULL);
}
}
//This is so it's not just a static window ie it can update
SDL_UpdateWindowSurface(window);
SDL_Delay(5000);
close();
return 0;
}
Problemmatic line is
if(!loadMedia){
It is not a function call. What it does is checks address of function loadMedia is NULL, which is always false because function address assigned by linker (as opposed to funciton pointers which you manually assign at runtime).
As you mentioned codeblocks, I suppose you use gcc to compile your code. If only you've added at least -Wall flag (which is a very good practice) compiler would have warned you that you're doing something that very much looks like a mistake:
test2.cpp:70:9: warning: the address of ‘bool loadMedia()’ will always evaluate as ‘true’ [-Waddress]

SDL_Texture is not being set

I am having troubles with SDL_Texture
RPGTutorial.cpp
#include "stdafx.h"
int main(int argc, char *argv[])
{
bool quit = false;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = NULL;
window = SDL_CreateWindow("RPG Tutorial!", 100, 100, 600, 400, SDL_WINDOW_SHOWN);
if (window == NULL)
{
std::cout << "Window couldn't be created" << std::endl;
return 0;
}
SDL_Renderer* renderer = NULL;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
std::cout << "Renderer is not being created!" << std::endl;
SDL_DestroyWindow(window);
system("PAUSE");
return 0;
}
SDL_Event* mainEvent = new SDL_Event();
SDL_Texture* grass = NULL;
grass = IMG_LoadTexture(renderer, "Grass.bmp");
if (grass == NULL)
{
std::cout << "Grass Image was not found!" << std::endl;
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
delete mainEvent;
system("PAUSE");
return 0;
}
SDL_Rect grass_rect;
grass_rect.x = 0;
grass_rect.y = 0;
grass_rect.w = 64 * 2;
grass_rect.h = 64 * 2;
while (!quit && mainEvent->type != SDL_QUIT)
{
SDL_PollEvent(mainEvent);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, grass, NULL, &grass_rect);
SDL_RenderPresent(renderer);
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
delete mainEvent;
return 0;
}
stdafx.h
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <iostream>
I have the Grass.bmp in the RPGTutorial->RPGTutorial->Grass.bmp
When I compile it, it is successful. It runs through the code til I get to the part where it checks if(grass == NULL) and it goes through that and exits. Can someone help me know why my grass is not being set to the image when I have the image in the same folder that the .cpp files are in? I even tried adding an Image folder to hold it in, and it did not work either.
If you have the time, I recommend you take some time to go through the Lazyfoo tutorials they are fantastic. He mentions this issue in the second tutorial, "Getting an Image on the Screen."
Visual Studio changes your current working directory to the place where your .vcxproj file is. That will be the directory you want to place your resources in. If you're not sure where that is, you can use the _getcwd() function in the direct.h header MSDN Source For getcwd