Cant execute a simple "SDL2HelloWorld" on Windows with CMake - c++

I don't understand why when I run the program nothing happens (no windows or cout):
.\src\main.cpp :
#include <SDL2/SDL.h>
#include <iostream>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
int main(int argc, char* args[]) {
std::cout << "Le programme se lance"; // should at least show
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "could not initialize sdl2 :" << SDL_GetError() << std::endl;
return 1;
}
window = SDL_CreateWindow(
"hello_sdl2",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN
);
if (window == NULL) {
std::cout << "could not create window:" << SDL_GetError() << std::endl;
return 1;
}
screenSurface = SDL_GetWindowSurface(window);
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
SDL_UpdateWindowSurface(window);
SDL_Delay(10000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
.\CMakeLists.txt :
cmake_minimum_required(VERSION 3.25.1)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
project("Test")
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(SDL2_DIR "${SRC_DIR}/lib/cmake/SDL2")
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(MAIN "${SRC_DIR}/src/main.cpp")
target_link_libraries(MAIN ${SDL2_LIBRARIES})
The architecture of my project is the following :
All files are from the assets SDL2-devel-2.26.1-mingw.zip
The commands I used in order :
cd .\build\
cmake .. -G "MinGW Makefiles"
make
.\MAIN.exe
make and .\Main.exe results :

In fact I just had to copy SDL2.dll in the build folder.

Related

I can't access my main when I include the SDL_image.h

This is my code:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_version.h>
#include <SDL2/SDL_image.h>
const int WIDTH = 800, HEIGHT = 600;
int main( int argc, char *argv[] )
{
std::cout << "can't access main\n";
if (SDL_Init (SDL_INIT_EVERYTHING) < 0){
std::cout << "SDL Init Error: " << SDL_GetError() << '\n';
}
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)){
std::cout << "Fail to Init SDL_Image for .PNG Error: " << IMG_GetError() << '\n';
}
SDL_Surface *imgSur = NULL;
SDL_Surface *window1_Sur = NULL;
SDL_Rect *destR;
destR -> x = 0;
destR -> y = 0;
destR -> h = 0;
destR -> w = 0;
SDL_Window *window1 = SDL_CreateWindow("Hewwo >///<",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
WIDTH, HEIGHT,
SDL_WINDOW_ALLOW_HIGHDPI);
if (window1 == NULL){
std::cout << "Fail to create window: " << SDL_GetError() << '\n';
return EXIT_FAILURE;
}
imgSur = IMG_Load("data/pictures/Kohaku_Small.png");
// imgSur = SDL_LoadBMP("data/pictures/Kohaku_Small.bmp");
if (imgSur == NULL){
std::cout << "Error loading data. SDL Error: " << SDL_GetError() << '\n';
}
else std::cout << "meh\n";
SDL_Renderer *renderer;
renderer = SDL_CreateRenderer(window1, -1, 0);
SDL_Texture *txtr1 = SDL_CreateTextureFromSurface(renderer, imgSur);
int imgH = imgSur -> h;
int imgW = imgSur -> w;
destR -> h = imgH;
destR -> w = imgW;
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
SDL_Event window1_Event;
bool quit = 0;
while (!quit){
if (SDL_PollEvent( &window1_Event)){
if (window1_Event.type == SDL_QUIT){
quit = 1;
}
}
SDL_RenderClear( renderer );
SDL_RenderCopy(renderer, txtr1, NULL, destR);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(txtr1);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window1);
SDL_Quit();
return EXIT_SUCCESS;
}
this is my makefile:
Copycat:
g++ -I src/include -L src/lib -o Copycat Copycat.cpp -lmingw32 -lSDL2main -lSDL2 -lSDL2_image
when I compile with mingw32-make in VSCode it doesn't return any error and nothing happens at all
I tried debug something to find the problem and turned out it was the #include <SDL2/SDL_image.h> that prevent me from accessing the 'main' function as it can't print out the text. I tried reinstall SDL2 but it still the same, can somebody help me fix this problem?

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_image as a subproject with cmake failing

I am trying to use SDL and SDL_image as a subprojects in my project. Folder structure:
testRepos
├── build //build files inside
├── src
│ ├── main.cpp
│ ├── texture.png
│ └── CMakeLists.txt
├── third_party
│ ├── SDL //SDL repo inside
│ ├── SDL_image //SDL_image repo inside
│ └── CMakeLists.txt
└── CMakeLists.txt
main.cpp
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
using namespace std;
SDL_Window *m_window;
SDL_Renderer *m_renderer;
bool init();
void close();
SDL_Texture* loadTexture(const char* file_);
void renderTexture(SDL_Texture* tex_, int x_, int y_, int w_, int h_);
void renderTexture(SDL_Texture* tex_, int x_, int y_);
int SDL_main(int argc, char* args[])
{
if (!init())
{
cout << "Cannot init SDL\n";
}
bool isRunning = true;
SDL_Event e;
SDL_Texture* tex = loadTexture("texture.png");
while (isRunning)
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
isRunning = false;
}
SDL_RenderClear(m_renderer);
renderTexture(tex, 0, 0);
SDL_RenderPresent(m_renderer);
}
close();
system("pause");
return 0;
}
bool init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cout << "SDL initialization error: " << SDL_GetError() << std::endl;
return false;
}
if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG)
{
std::cout << "IMG initialization error: " << SDL_GetError() << std::endl;
return false;
}
m_window = SDL_CreateWindow("TestName", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_SHOWN);
if (m_window == NULL)
{
std::cout << "Window creation error: " << SDL_GetError() << std::endl;
return false;
}
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
if (m_renderer == nullptr) {
std::cout << "Renderer creation error: " << SDL_GetError() << std::endl;
return false;
}
}
void close()
{
SDL_DestroyRenderer(m_renderer);
SDL_DestroyWindow(m_window);
IMG_Quit();
SDL_Quit();
}
SDL_Texture* loadTexture(const char* file_)
{
SDL_Texture* texture = IMG_LoadTexture(m_renderer, file_);
if (!texture) {
std::cout << "Texture loading problem: " << file_ << " | " << IMG_GetError() << std::endl;
}
return texture;
}
void renderTexture(SDL_Texture* tex_, int x_, int y_, int w_, int h_)
{
SDL_Rect dst;
dst.x = x_;
dst.y = y_;
dst.w = w_;
dst.h = h_;
SDL_RenderCopy(m_renderer, tex_, NULL, &dst);
}
void renderTexture(SDL_Texture* tex_, int x_, int y_)
{
int w, h;
SDL_QueryTexture(tex_, NULL, NULL, &w, &h);
renderTexture(tex_, x_, y_, w, h);
}
testRepos/CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
project (ProjectRoot)
add_subdirectory (third_party)
add_subdirectory (src)
testRepos/third_party/CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
project(third_party)
add_subdirectory(SDL)
set(SDL_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/SDL/include)
add_subdirectory(SDL_image)
set(SDL_IMAGE_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/SDL_image)
testRepos/src/CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
project(main)
add_executable (main main.cpp)
# Connecting the library, specify where to get the header files
target_include_directories(main PUBLIC ${SDL_INCLUDE_DIR} ${SDL_IMAGE_INCLUDE_DIR})
# And also we specify dependence on static library
target_link_libraries(main SDL2main SDL2-static SDL2_image)
It works perfectly fine without SDL_image (ofc without SDL_image code in sources). After adding SDL_image, it still builds, but requires to manually add SDL2_image.dll, SDL2d.dll , zlibd1.dll and libpng16d.dll and fails after running .exe with this:
WARN:
Assertion failure at SDL_CreateTextureFromSurface_REAL (T:\testRepos\third_party\SDL\src\render\SDL_render.c:1252), triggered 1 time:
'renderer && renderer->magic == &renderer_magic'
Texture loading problem: texture.png |
WARN:
Assertion failure at SDL_QueryTexture_REAL (T:\testRepos\third_party\SDL\src\render\SDL_render.c:1390), triggered 1 time:
'texture && texture->magic == &texture_magic'
WARN:
Assertion failure at SDL_RenderCopyF_REAL (T:\testRepos\third_party\SDL\src\render\SDL_render.c:3199), triggered 1 time:
'texture && texture->magic == &texture_magic'
It fails inside the IMG_LoadTexture, so everything definitely was initialized properly. Since I use libraries as a subprojects, I cannot use solution with packages. How can I solve this problem?

C++ SDL2 window not opening

i coded this.
#include <iostream>
#include "SDL.h"
int main(int argc , char** args)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* win = SDL_CreateWindow("my window", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (!win)
{
std :: cout << "Failed to create a window! Error: " << SDL_GetError() << "\n";
}
SDL_Surface* winSurface = SDL_GetWindowSurface(win);
SDL_UpdateWindowSurface(win);
SDL_FillRect(winSurface, NULL, SDL_MapRGB(winSurface->format, 255, 90, 120));
SDL_DestroyWindow(win);
win = NULL;
winSurface = NULL;
return 0;
}
when i compile it, it opens the window, and it immediately closes. But the console doesn't. Here is a screenshot of my console(maybe it could help solving the problem?)
Would there be any solution to get the Window to not close?
Would there be any solution to get the Window to not close?
Start up an event-handling loop and handle some events:
// g++ main.cpp `pkg-config --cflags --libs sdl2`
#include <SDL.h>
#include <iostream>
int main( int argc, char** argv )
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* win = SDL_CreateWindow("my window", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (!win)
{
std :: cout << "Failed to create a window! Error: " << SDL_GetError() << "\n";
}
SDL_Surface* winSurface = SDL_GetWindowSurface(win);
bool running = true;
while( running )
{
SDL_Event ev;
while( SDL_PollEvent( &ev ) )
{
if( ( SDL_QUIT == ev.type ) ||
( SDL_KEYDOWN == ev.type && SDL_SCANCODE_ESCAPE == ev.key.keysym.scancode ) )
{
running = false;
break;
}
}
SDL_FillRect(winSurface, NULL, SDL_MapRGB(winSurface->format, 255, 90, 120));
SDL_UpdateWindowSurface(win);
}
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}

c++ with SDL not running

i've installed SDL to develop a program, and while on the test phase for this lib, the compiled code does not execute, and i mean it won't even open the cmd box. The weird thing is, it occasionally executes.
I'm using Eclipse Hellios with minGW32 and i686-w64-mingw32 on windows10.
My test code is:
#include <iostream>
#include <Windows.h>
#include <SDL2/SDL.h>
int main(int, char**)
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return 1;
SDL_Window *window = SDL_CreateWindow("", 300, 100, 1024, 800, SDL_WINDOW_OPENGL);
if (window == NULL)
{
std::cout << "SDL init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface *background = SDL_LoadBMP("Resources/Lilothyn.bmp");
if (background == NULL)
{
SDL_ShowSimpleMessageBox(0, "Background init error", SDL_GetError(), window);
return 1;
}
if (renderer == NULL)
{
SDL_ShowSimpleMessageBox(0, "Renderer init error", SDL_GetError(), window);
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, background);
if (texture == NULL)
{
SDL_ShowSimpleMessageBox(0, "Texture init error", SDL_GetError(), window);
}
SDL_RenderPresent(renderer);
SDL_Event event;
bool running = true;
while(running)
{
SDL_PollEvent(&event);
switch(event.type)
{
case SDL_QUIT:
running = false;
SDL_DestroyTexture(texture);
SDL_FreeSurface(background);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
break;
default: break;
}
}
return 0;
}
the odd thing is, i previously had "SDL_LoadBMP("Resources/TommyBMP.bmp");" and it executed properly. Changing a string shouldn't make the program stop working, both files are at the Debug/Resources folder so it can't be a case of not finding them...
note: further testing shows that a simple "hello world" has the same issue, so the problem will likely not be from SDL.
"help me obi wan, you're my only hope".