Program building correctly but not running - c++

This is my code:
#include <SDL2/SDL.h>
#include <stdio.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main(int argc, char* args[])
{
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
if (SDL_Init(SDL_INIT_VIDEO ) < 0 )
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
window = SDL_CreateWindow("Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if (window == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
}
else
{
screenSurface = SDL_GetWindowSurface(window);
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF ));
SDL_UpdateWindowSurface(window);
SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
}
}
The program builds all fine no errors no warnings no nothing. But when it comes to running it. My window pc says This app can't run on your pc. Does anyone know how to fix this?
The build system i am using is gcc mingw.
COMMAND:
g++ Test.cpp -I"include" -L"lib" -Wall -lmingw32 -lSDL2main -lSDL2 -mwindows -o Test.exe

Related

Can't draw simple bitmap to window in SDL2

I'm currently trying to set up a few C++ libraries for a future project. Namely, SDL2.
Here's my code:
#include <iostream>
#include <fstream>
#include <SDL.h>
int SCREEN_WIDTH = 457;
int SCREEN_HEIGHT = 497;
const char* imgpath = "Sprite.bmp";
std::string errmsg;
SDL_Window* window = NULL;
SDL_Surface* screensurface = NULL;
SDL_Surface* image = NULL;
SDL_Rect rect;
struct {
bool wdown;
bool adown;
bool sdown;
bool ddown;
bool edown;
bool escdown;
} kpresses;
void clearkpresses() {
kpresses.wdown = false;
kpresses.adown = false;
kpresses.sdown = false;
kpresses.ddown = false;
kpresses.edown = false;
kpresses.escdown = false;
}
void setrect() {
rect.x = 0;
rect.y = 0;
rect.w = 457;
rect.h = 497;
}
bool gameinit() {
std::ofstream errfile;
errfile.open("errlog.txt");
clearkpresses();
setrect();
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0 ) {
errmsg = "SDL could not initialize! SDL_Error:";
std::cout << errmsg << SDL_GetError() << std::endl;
errfile << errmsg << SDL_GetError() << std::endl;
errfile.std::ofstream::close();
return false;
}
screensurface = SDL_GetWindowSurface(window);
window = SDL_CreateWindow("Transcend", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if( window == NULL ){
errmsg = "Window could not be created! SDL_Error:";
std::cout << errmsg << SDL_GetError() << std::endl;
errfile << errmsg << SDL_GetError() << std::endl;
errfile.std::ofstream::close();
return false;
}
image = SDL_LoadBMP(imgpath);
if (image == NULL) {
errmsg = "Media unable to load! IMG Error: ";
std::cout << errmsg << SDL_GetError() << std::endl;
errfile << errmsg << SDL_GetError() << std::endl;
errfile.std::ofstream::close();
return false;
}
return true;
}
void gamehalt()
{
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
int main(int argc, char* args[]) {
if (!gameinit()) {
gamehalt();
return 0;
}
bool quit = false;
SDL_Event event;
while (!quit) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
quit = true;
}
switch(event.type) {
case SDLK_w:
kpresses.wdown = true;
case SDLK_a:
kpresses.adown = true;
case SDLK_s:
kpresses.sdown = true;
case SDLK_d:
kpresses.ddown = true;
case SDLK_e:
kpresses.edown = true;
case SDLK_ESCAPE:
kpresses.escdown = true;
}
}
//TODOUpdate
//TODORender
SDL_BlitSurface(image, NULL, screensurface, &rect);
SDL_UpdateWindowSurface(window);
//reset
clearkpresses();
}
gamehalt();
return 0;
}
Compiled, assembled, and linked with this windows cmd command:
g++ Main.cpp -static-libgcc -static-libstdc++ -I..\include\SDL2\include\SDL2 -I..\include\SDL2_image\include\SDL2 -L..\include\SDL2\lib -L..\include\SDL2_image\lib -w -lmingw32 -lSDL2main -lSDL2 -o ../Transcend.exe
It compiles and runs with no errors, but only displays a blank screen.
The problem is that you are trying to get the window surface, but the window isn't already created.
Please swap those two lines:
screensurface = SDL_GetWindowSurface(window);
window = SDL_CreateWindow("Transcend", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
This way:
window = SDL_CreateWindow("Transcend", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
screensurface = SDL_GetWindowSurface(window);

SDL fails to display image?

My code doesn't display anything. All I get is a window with no image.
#include <iostream>
#include <stdio.h>
#include <SDL2/SDL.h>
using namespace std;
SDL_Window *gWindow=NULL;
SDL_Surface *gScreenSurface=NULL;
SDL_Surface *gHelloWorld=NULL;
const int SCREEN_WIDTH=640, SCREEN_HEIGHT=480;
bool init(){
bool success = true;
if(SDL_Init (SDL_INIT_VIDEO) < 0 ) {
printf("SDL could not initialize! SDL_Error : %s \n", SDL_GetError() );
success=false;
}
else{
gWindow = SDL_CreateWindow ( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL ){
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success=false;
}
else {
gScreenSurface = SDL_GetWindowSurface (gWindow);
}
}
return success;
}
bool loadMedia(){
bool success=true;
gHelloWorld = SDL_LoadBMP ( "hello_world.bmp" );
if (gHelloWorld == NULL ){
printf( "Unable to load image %s! SDL Error: %s\n", "hello_world.bmp", SDL_GetError() );
success=false;
}
return success;
}
void close(){
SDL_FreeSurface( gHelloWorld );
gHelloWorld=NULL;
SDL_DestroyWindow( gWindow );
gWindow=NULL;
SDL_Quit();
}
int main(int argc, char* args[]){
if(!init()){
printf( "failed to initialize!\n" );
}
else {
if( !loadMedia() ) {
printf ("failed to laod media! \n");
}
else {
SDL_BlitSurface( gHelloWorld, NULL, SDL_GetWindowSurface(gWindow), NULL );
SDL_UpdateWindowSurface ( gWindow );
SDL_Delay (2000);
}
}
close();
I expect it to show me a bmp image which is in the path specified here in the loadBMP() function but all I get is an empty transparent window.
I am using KDE Konsole, if that has to do something with this.
KDE eh? Plasma composites by default; try disabling compositing or add a proper event-handling loop so your process has a chance to handle repaint events.

SDL-Image: Couldn't open image

Unfortunately I do not get a picture but only a white screen. I am currently learning c ++ and sdl.
error message:
SDL_image Error: Couldn't open test.jpg
I am using Visual Studio on a Windows 10 computer.
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;;
string ExePath() {
char buffer[MAX_PATH];
GetModuleFileName(NULL, buffer, MAX_PATH);
string::size_type pos = string(buffer).find_last_of("\\/");
return string(buffer).substr(0, pos);
}
int main(int argc, char* args[]) {
cout << "my directory is " << ExePath() << "\n";
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
std::cout << "SDL load fail" << std::endl;
return -1;
}
SDL_Window* window = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_SHOWN);
if (window == NULL) {
std::cout << "Window load fail." << std::endl;
return -1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
std::cout << "Renderer load fail." << std::endl;
return -1;
}
SDL_Texture* background = IMG_LoadTexture(renderer, "test.jpg");
if (background == NULL) {
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
SDL_Delay(5000);
return -1;
}
SDL_Rect pos;
pos.x = 20;
pos.y = 30;
pos.w = 460;
pos.h = 300;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, background, NULL, &pos);
SDL_RenderPresent(renderer);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
atexit(SDL_Quit);
return 0;
}
I have already tried the absolute path. The picture is also in the correct folder. Smaller pictures, jpeg, png. I do not know.
SDL Image is a separate library. You should initialize it befor using it. Something like this:
////...
int imgFlags = IMG_INIT_JPG; // or IMG_INIT_PNG;
// not sure about the imgFlags parameter, read the docs.
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
}
/// the rest is the same...
SDL_Texture* background = IMG_LoadTexture(renderer, "test.jpg");
if (background == NULL) {
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
SDL_Delay(5000);
return -1;
}
/// ...
Some reference https://discourse.libsdl.org/t/help-with-initializing-sdl-image/23601
EDIT: And for the "one step closer": I think your delay is in the wrong place.
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, background, NULL, &pos);
SDL_RenderPresent(renderer);
SDL_Delay(5000); //////// For example try putting int here
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
atexit(SDL_Quit);
std::cout << "Bye!" << std::endl;
return 0;

SDL_SetPaletteColors crashes program

The program is supposed take pixel data from a uni dimensional array and display it. The pixel data is supposed to be 1 byte per pixel, which is supposed to result in a gray scale image.
The result is supposed to look like this:
Th problem i run into is that the "SDL_SetPaletteColors" command crashes the program.
What am I doing wrong here?
Here is the code:
#include <SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 100;
const int SCREEN_HEIGHT = 100;
char* pixels;
int icnt,icnt2;
//Starts up SDL and creates window
bool init();
//Frees media and shuts down SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load and show on the screen
//SDL_Surface* gHelloWorld = NULL;
SDL_Surface* gHelloWorld = NULL;
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}
return success;
}
void close()
{
//Deallocate surface
SDL_FreeSurface( gHelloWorld );
gHelloWorld = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main( int argc, char* args[] )
{
pixels = new char[10000]; //pixel array
icnt2=0;
SDL_Color colors[256];
for(icnt=0;icnt<10000;icnt++) //gradient test image generator
{
pixels[icnt]=(char)icnt2;
icnt2++;
if(icnt2 == 99){icnt2 =0;}
}
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
gHelloWorld = SDL_CreateRGBSurfaceFrom((void*)pixels,
100,
100,
8,
100,
0x000000FF,
0x0000FF00,
0x00FF0000,
0);
for(icnt = 0; icnt < 255; icnt++)
{
colors[icnt].r = colors[icnt].g = colors[icnt].b = icnt;
}
//program crashes here ------------------
SDL_SetPaletteColors(gHelloWorld->format->palette, colors, 0, 255); //program crashes here
//-----------------------------------------
//Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
//Wait two seconds
SDL_Delay( 2000 );
//}
}
//Free resources and close SDL
close();
return 0;
}

Can't Close Window

I'm following the Lazy Foo tutorial on C++ and SDL2. I'm trying to learn it using regular C and noticed something interesting when following instructions on adding events to detect a close window event.
Here is the code.
#include <stdio.h>
#include <stdbool.h>
#include "SDL2/SDL.h"
bool init();
bool loadMedia();
void close();
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window *gWindow = NULL;
SDL_Surface *gScreenSurface = NULL;
SDL_Surface *gHelloWorld = NULL;
int main(int argc, char *argv[])
{
if(!init()) {
printf("Failed to initialize!\n");
}
else {
if(!loadMedia()) {
printf("Failed to load media!\n");
}
else {
bool quit = false;
SDL_Event e;
while(!quit) {
printf("%d", SDL_PollEvent(&e));
while(SDL_PollEvent(&e) != 0) {
if(e.type == SDL_QUIT) {
quit = true;
}
}
SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface(gWindow);
}
}
}
close();
return 0;
}
bool init()
{
bool success = true;
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else {
gWindow = SDL_CreateWindow("SDL Tutorial 03", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(gWindow == NULL) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else {
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
}
return success;
}
bool loadMedia()
{
bool success = true;
gHelloWorld = SDL_LoadBMP("images/hello_world.bmp");
if(gHelloWorld == NULL) {
printf("Unable to load image %s! SDL_Error: %s\n", "images/hello_world.bmp", SDL_GetError());
success = false;
}
return success;
}
void close()
{
SDL_FreeSurface(gHelloWorld);
gHelloWorld = NULL;
SDL_DestroyWindow(gWindow);
gWindow = NULL;
SDL_Quit();
}
If I compile this with a ".c" extension, it compiles without errors, but selecting the "X" on the window title bar does nothing. If I change said extension to ".cpp", the "X" works as intended.
I'm using the following command to compile the code.
gcc main.c -w -lSDL2 -o main
Any ideas why this may work with C++, but not with C?
The function SDL_PollEvent will remove the event from the internal event queue if an address of an SDL_event object is passed to it.
The printf call, that also calls the function SDL_PollEvent, before the event loop which will remove the quit event from the queue. This means the event loop won't find this event:
printf("%d", SDL_PollEvent(&e));
while(SDL_PollEvent(&e) != 0) {
if(e.type == SDL_QUIT) {
quit = true;
}
}
If you only want to check if there are events pending in the queue, then use the function SDL_PollEvent with a NULL argument:
printf("%d", SDL_PollEvent(NULL));