simple code the image pointer are NULL why?:
// defined in the header files
SDL_Surface* m_imageIcon;
SDL_Surface* m_CharMainTile;
bool SDLmanager::loadFiles()
{
m_imageIcon = loadImage("D:\\java\\workspace\\SDL_test1\\Debug\\robot1.bmp",type_bmp);
/*
mem print as you see empty
Name : m_imageIcon
Details:0x0
Default:0x0
Decimal:0
Hex:0x0
Binary:0
Octal:0
*/
m_CharMainTile = loadImage("D:\\java\\workspace\\SDL_test1\\Debug\\vx_chara09_d.png",type_png);
bool b = false;
if(m_imageIcon == NULL) // this is null
b = true;
if(m_CharMainTile == NULL) // this is null
b = true;
if(!b)
return false;
return true;
}
this is where i set the files there all vaild and in the given path
SDL_Surface *SDLmanager::loadImage( string filename , imgType t)
{
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
if(t== type_bmp)
{
loadedImage = SDL_LoadBMP( filename.c_str() );
}
else
{
loadedImage = IMG_Load( filename.c_str() );
}
//If nothing went wrong in loading the image
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
}
//Return the optimized image
return optimizedImage;
}
optimizedImage has vaild address and value
Name : optimizedImage
Details:0x4d6ea8
Default:0x4d6ea8
Decimal:5074600
Hex:0x4d6ea8
Binary:10011010110111010101000
Octal:023267250
compilation is fine and clean :
make all
Building file: ../Main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g -Wall -c -fmessage-length=0 -MMD -MP -MF"Main.d" -MT"Main.d" -o "Main.o" "../Main.cpp"
Finished building: ../Main.cpp
Building file: ../SDLmanager.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g -Wall -c -fmessage-length=0 -MMD -MP -MF"SDLmanager.d" -MT"SDLmanager.d" -o "SDLmanager.o" "../SDLmanager.cpp"
Finished building: ../SDLmanager.cpp
Building target: SDL_test1.exe
Invoking: MinGW C++ Linker
g++ -o "SDL_test1.exe" ./Main.o ./SDLmanager.o -lmingw32 -lSDL_image -lSDLmain -lSDL
Finished building target: SDL_test1.ex
Related
When i Call SDL_Init passing in SDL_AUDIO_INIT, the function returns a failure.
I called SDL_GetError() but there does not seem to be any message.
I am not having problems getting VIDEO to initialize, only audio. I don't get any errors when compiling. I have linked the SDL mixer library in my makefile.
Here is a minimum reproducible example code:
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string>
int main( int argc, char* args[] )
{
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO < 0))
{
printf( "SDL VIDEO could not initialize! SDL Error: %s\n", SDL_GetError() );
}
else if ( SDL_Init( SDL_INIT_AUDIO) < 0)
{
printf( "SDL AUDIO could not initialize! SDL Error: %s\n", SDL_GetError() );
}
else
{
printf("video and audio initialized");
}
return 0;
}
output: SDL AUDIO could not initialize! SDL Error:
(there is no message for the error)
Here is the makefile
#OBJS specifies which files to compile as part of the project
OBJS = 21_sound_effects_and_music.cpp
#CC specifies which compiler we're using
CC = g++
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -g -Wall
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = sdl_program
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
I am using Ubuntu 20.04
install libasound2-dev libpulse-dev
and recompyling SDL2
Audio drivers : disk dummy oss alsa(dynamic) pulse(dynamic) <--- IS OK
I am attempting to set up SDL2 for creating a C++ game for windows. When I run the below make file with just a single source file, I get no errors, and the program can run. When I attempt to run the make file with multiple source files, the compilation goes smoothly, but when I attempt to run the program, i get the error seen in the title. It appears to only happen when I attempt to create an instance of a class in the second source file, and try to access either members or instance functions.
I have tried redownloading and setting up my environment, updating mingw (didnt hurt to try) and triple checking my code. I have included the makefile, as well as the source files I am using. Thank you for any help.
EDIT: It seems that the delete call in the main function causes this error... Not sure if that is a coincidence or if the cause does lie somewhere there...
#OBJS specifies which files to compile as part of the project
OBJS = src/*.cpp src/*.hpp
#CC specifies which compiler we're using
CC = g++
#INCLUDE_PATHS specifies the additional include paths we'll need
INCLUDE_PATHS = -Iinclude/SDL2
#LIBRARY_PATHS specifies the additional library paths we'll need
LIBRARY_PATHS = -Llib
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
# -Wl,-subsystem,windows gets rid of the console window
COMPILER_FLAGS =
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = demoGame
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
#include <SDL.h>
#include <stdio.h>
#include "grid.hpp"
#include <iostream>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main( int argc, char* args[] )
{
//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 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create window
window = SDL_CreateWindow( "SDL Tutorial", 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
{
//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 );
//Wait two seconds
SDL_Delay( 2000 );
}
}
Grid* grid = new Grid(1, 2);
// These two lines seem to cause an issue.
std::cout << grid->getRows() << std::endl;
delete grid;
//Destroy window
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}
class Grid {
public:
Grid(int rows, int columns) {
this->rows = rows;
this->columns = columns;
}
int getRows() {
return this->rows;
}
private:
int rows;
int columns;
};
I've been struggling with getting SDL and SDL_ttf to work together. My minimal reproducible case (down below) works in VC++ but for the life of me I can't get it to work in a MinGW environment.
SDL version: 2.0.10 (latest at moment of writing)
SDL_ttf version: 2.0.15 (latest at moment of writing)
My set up looks like this:
Copied SDL and SDL_ttf lib and include folders to my project root.
Set up my link and include in my Makefile like so:
SRC_DIR=./app/src
INC_DIR=./app/inc
OBJ_DIR=./.obj
BUILD_DIR=./build
NAME=$(BUILD_DIR)/out
all: $(NAME)
CC=gcc
CXX=g++
CXXFLAGS=-Wall -Wextra -pedantic --std=c++17 -g
# Objects to build
STNAMES=\
main \
OBJECTS=$(patsubst %,$(OBJ_DIR)/%.o, $(STNAMES))
# Windows specific settings
ifeq ($(OS), Windows_NT)
LINK=\
-L./SDL2/lib \
-L./SDL2_ttf/lib \
-lmingw32 \
-lSDL2main \
-lSDL2 \
-lSDL2_ttf \
-lstdc++ \
INCLUDE=\
-I$(INC_DIR) \
-I./SDL2/include \
-I./SDL2_ttf/include \
define mkdir =
if not exist "$(1)" mkdir "$(1)"
endef
define rmdir =
if exist "$(1)" del /S/Q "$(1)"
endef
EXECUTABLE=$(NAME).exe
# macOS specific settings
# UNTESTED, assumes dependencies are installed with brew
else
LINK=$(shell sdl2-config --libs) -lc++
INCLUDE=\
-I$(shell brew --prefix)/include/SDL2 \
define mkdir
mkdir -p $(1)
endef
define rmdir =
rm -rf $(1)
endef
EXECUTABLE=$(NAME)
endif
$(NAME): $(OBJECTS)
#$(call mkdir,$(BUILD_DIR))
$(CC) -o $# $(OBJECTS) $(LINK)
#$(EXECUTABLE)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
#$(call mkdir,$(OBJ_DIR))
#$(CXX) -o $# -c $< $(CXXFLAGS) $(INCLUDE)
re:
#$(call rmdir,$(OBJ_DIR))
make
Added the dlls to MinGW's bin folder.
Here's a minimal reproducible test case which works for me on Visual Studio, but not on MinGW:
#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>
void drawText(SDL_Renderer* renderer, TTF_Font* font, int x, int y, const char* text) {
auto surface = TTF_RenderText_Solid(font, text, SDL_Color{ 255, 255, 255, 255 });
auto texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect dstRect = { x, y, surface->w, surface->h };
SDL_RenderCopy(renderer, texture, NULL, &dstRect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("some title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1920, 1080, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
TTF_Init();
bool running = true;
SDL_Event event;
auto font = TTF_OpenFont("assets/courier_prime.ttf", 36);
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
running = false;
}
SDL_SetRenderDrawColor(renderer, 25, 25, 25, 255);
SDL_RenderClear(renderer);
drawText(renderer, font, 10, 10, "Test string");
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255 );
auto rect = SDL_Rect{10, 100, 110, 200};
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer);
}
TTF_CloseFont(font);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
}
Regardless of where I place that SDL_RenderFillRect (before or after the drawText), the only thing I see is the text. The Rectangle is no longer visible.
I'm currently dealing with a mixed OS dev environment (mac and windows), hence why I'd like it to build on both using make.
Seems like it was an issue with SDL_RENDERER_ACCELERATED and openGL not being linked. Linking openGL did not fix the issue, however, swapping to the software renderer (SDL_RENDERER_SOFTWARE) did fix the rendering bug. Very strange that it does work on VS though. I guess that'll remain a mystery...
i'm trying to use the SDL1.2 librairie with c++ files but, i can't compile.
i'm on windows and when i want to compile, the linking cannot be done.
i made a makefile :
CC = gcc
CFLAGS = -Wall -I include
LDFLAGS = -L/lib -lmingw32 -lSDLmain -lSDL -lSDL_image -mwindows
Programme : main.o
$(CC) main.o -o Programme $(LDFLAGS)
main.o : main.cpp
$(CC) $(CFLAGS) -c main.cpp -o main.o
clean :
del -rf *.o
mrproper : clean
del Programme
and here is my main.cpp :
#include <stdio.h>
#include <SDL/SDL.h>
void pause();
int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL, *imageDeFond = NULL;
SDL_Rect positionFond;
positionFond.x = 0;
positionFond.y = 0;
SDL_Init(SDL_INIT_VIDEO);
ecran = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
SDL_WM_SetCaption("Chargement d'images en SDL", NULL);
imageDeFond = SDL_LoadBMP("lac_en_montagne.bmp");
SDL_BlitSurface(imageDeFond, NULL, ecran, &positionFond);
SDL_Flip(ecran);
pause();
SDL_FreeSurface(imageDeFond);
SDL_Quit();
return EXIT_SUCCESS;
}
void pause()
{
int continuer = 1;
SDL_Event event;
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
}
}
}
and when i run the makefile with the command :
mingw32-make
here is the output :
C:\Users\summire\Desktop\a day in hell>mingw32-make
gcc main.o -o Programme -L/lib -lmingw32 -lSDLmain -lSDL -lSDL_image -mwindows
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDLmain
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL_image
collect2.exe: error: ld returned 1 exit status
mingw32-make: *** [makefile:6: Programme] Error 1
can someone please explain me what am i doing wrong ?
also, here is my arborescence :
/
img/
example1.png
include/
SDL/
all the .h files for SDL
lib/
libSDL.a
libSDLmain.a
src/
(nothing yet)
I just started learning c++ and makefiles. Now I'm stuck.
There seem to be dozens of questions like this, but I can't figure out which answer applies to my situation. This answer is probably obvious. Some clues for where to look and reasons for what I am doing is wrong would be greatly appreciated!
This is my error:
Undefined symbols for architecture x86_64:
"App::init(char const*, int, int, int, int, bool)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [../out/MYAPP] Error 1
This is my code:
main.cpp
#include "App.h"
// our App object
App* a_app = 0;
int main(int argc, char* argv[])
{
a_app = new App();
a_app->init("Hello World", 100, 100, 640, 480, true);
//...etc more code
return 0;
}
App.h
#ifndef __App__
#define __App__
#include <SDL2/SDL.h>
class App
{
public:
App(){}
~App(){}
bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
//..etc
private:
};
#endif /* defined(__App__) */
App.cpp
#include "App.h"
#include <iostream>
using namespace std;
bool App::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{
// attempt to initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
cout << "SDL init success \n";
int flags = 0;
if(fullscreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}
// init the window
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
//...etc
}
else
{
cout << "SDL init fail \n";
return false; // SDL init fail
}
cout << "SDL init success \n";
m_bRunning = true; // everything inited successfully, start the main loop
return true;
}
And finally my makefile
CXX = clang++
SDL = -framework SDL2 -framework SDL2_image
INCLUDES = -I ~/Library/Frameworks/SDL2.framework/Headers -I ~/Library/Frameworks/SDL2_image.framework/Headers
CXXFLAGS = -Wall -c -std=c++11 $(INCLUDES)
LDFLAGS = $(SDL) -F ~/Library/Frameworks/
SRC = src
BUILD = build
OUT = ../out
EXE = $(OUT)/MYAPP
OBJECTS = $(BUILD)/main.o $(BUILD)/App.o
all: $(EXE)
$(EXE): $(OBJECTS) | $(OUT)
$(CXX) $(LDFLAGS) $< -o $#
$(BUILD)/%.o : $(SRC)/%.cpp | $(BUILD)
$(CXX) $(CXXFLAGS) $< -o $#
$(BUILD):
mkdir -p $(BUILD)
$(OUT):
mkdir -p $(OUT)
clean:
rm $(BUILD)/*.o && rm $(EXE)
You are not linking all the object files.
$(CXX) $(LDFLAGS) $< -o $#
should be
$(CXX) $(LDFLAGS) $^ -o $#
since the $< pseudo-variable expands to only the first dependency, which is main.o. That matches with the linker error.
In fact, making this modification alone makes the error go away on my machine.