I am trying to use the SDL_image library so I can display a .png file in a window made using SDL2 library.
My problem is when ever I compile the code I get a lot of errors of about deceleration and stuff in the SDL header files. I'm using Eclipse on Ubuntu LTS 16.04
I am not entirely sure of what the problem is and how I can solve this so a step by step of what, why and how so I can fully understand it would be
great
Answer to question is in comments of this post.
Here are my Errors
In file included from /usr/include/SDL2/SDL.h:52:0,
from /usr/include/SDL2/SDL_image.h:27,
from temp.cpp:12:
/usr/include/SDL2/SDL_render.h:116:3: error: conflicting declaration
‘typedef enum SDL_RendererFlip SDL_RendererFlip’
} SDL_RendererFlip;
^
In file included from /usr/local/include/SDL2/SDL.h:52:0,
from temp.cpp:11:
/usr/local/include/SDL2/SDL_render.h:116:3: note: previous declaration
as
‘typedef enum SDL_RendererFlip SDL_RendererFlip’
} SDL_RendererFlip;
^
In file included from /usr/include/SDL2/SDL.h:57:0,
from /usr/include/SDL2/SDL_image.h:27,
from temp.cpp:12:
/usr/include/SDL2/SDL_version.h:51:16: error: redefinition of ‘struct
SDL_version’
typedef struct SDL_version
`
Here is my code
#include <iostream>
#include <string>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
using namespace std;
int main(int argc, char* argv[])
{
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Surface* surface;
SDL_Texture* texture;
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_JPG);
window = SDL_CreateWindow(
"SDL Window",
100,
600,
1200,
700,
SDL_WINDOW_OPENGL);
if (window == NULL)
{
cout << "Could not create window: \n" << SDL_GetError() <<
endl;
return 1;
}
renderer = SDL_CreateRenderer (window, -1, 0);
surface = IMG_Load("introtext.png");
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Delay(5000);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 0;
}
Related
I'm trying to set up an SDL project with Visual Studio 2019 using this article:
https://www.wikihow.com/Set-Up-SDL-with-Visual-Studio
but the compiler is throwing me the errors 'one or more multiply defined symbols found' and
'_main already defined in main.obj'.
main.obj is a file in the debug folder of my project but when I try deleting it or the entire debug folder, VS recreates it when I run the project.
I've read that c++ can't have more than one main function but I can't open the main.obj file and I don't really want to delete the one in main.cpp
Here's the code I'm running and thanks for your help!
#include "SDL.h"
#include <stdio.h>
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow
("An SDL2 window", // window's title
10, 25, // coordinates on the screen, in pixels, of the window's upper left corner
640, 480, // window's length and height in pixels
SDL_WINDOW_OPENGL);
SDL_Delay(3000); // window lasts 3 seconds
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Glad to know it works now. Maybe you had a messy file structure with your previous SDL installation. Anyways, I think it might be interesting to show how the SDL dependency can be removed from your main.cpp file, to avoid such problems in the future.
First, let's consider the example in your question. The example is not very useful in practice, but I'll show a better version after.
The main idea is hiding everything that has to do with SDL from your main.cpp and headers.
1. Simple Example
// MySDL.h - NO SDL STUFF
class MySDL
{
public:
MySDL() = default; // or whatever you need
void runtest() const; // here we'll run the 3sec window
};
Now, we can put all our SDL stuff in the cpp file:
// MySDL.cpp
#include "MySDL.h"
#include "SDL.h" // HERE WE INCLUDE SDL
void MySDL::runtest()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("yee haw", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
}
No SDL included in main.cpp, we just include our SDL interface MySDL.h.
// Now you can use your SDL interface like this
int main(int, char* [])
{
MySDL sdl;
sdl.runtest();
return 0;
}
2. Better Version
However, you would typically want something more sofisticated than a window which disappears in 3 seconds. Therefore, you might want to store class members which depends on SDL. But then, you would have to #include "SDL.h" in your MySDL.h header file, which would give you the same problems as described in your question and comments. To remove this dependency, we can use the pimpl idiom.
The header file now includes a pointer to our SDL implementation. This SDL implementation will be defined in the cpp file in order to remove the SDL dependency.
// MySDL.h
class MySDL
{
public:
MySDL() = default; // or whatever you need
~MySDL();
void doSmthWithYourWindow(/*args*/);
private:
// pointer to our SDLImplementation (defined in cpp file)
class SDLImplementation;
std::unique_ptr<SDLImplementation> _sdl;
};
In our cpp file, we define the SDLImplementation, and MySDL has access to that implementation through the _sdl pointer.
// MySDL.cpp
#include "MySDL.h"
#include "SDL.h"
// here we can store class members which depend on SDL
struct MySDL::SDLImplementation
{
SDLImplementation()
{
SDL_Init(SDL_INIT_EVERYTHING);
_window = SDL_CreateWindow("yee haw", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);
_renderer = SDL_CreateRenderer(_window, -1, 0);
SDL_SetRenderDrawColor(_renderer, 0, 255, 0, 255);
SDL_RenderClear(_renderer);
SDL_RenderPresent(_renderer);
}
// functionality of your SDL implementation
void turnWindowUpsideDown() { /* _window->turnUpsideDown(); */ }
// members depending on SDL
SDL_Window* _window;
SDL_Renderer* _renderer;
};
MySDL::~MySDL() = default;
void MySDL::doSmthWithYourWindow(/*args*/)
{
// here we have access to our SDL implementation
_sdl->turnWindowUpsideDown();
}
Just like before, we only include our MySDL.h interface in the main.cpp file.
int main(int, char* [])
{
MySDL sdl;
sdl.doSmthWithYourWindow();
return 0;
}
So I ended up deleting SDL and completely restarting with a different tutorial linked here:
https://www.youtube.com/watch?v=QQzAHcojEKg
Not really sure what the difference was but it worked. Anyways, thanks for your help and I'll put the new code here.
#include "SDL.h"
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("yee haw", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
return 0;
}
I have been trying to make a PNG image appear on-screen to my SDL window. I am using the Eclipse CDT. SDL.h and SDL_image.h both seem to have been correctly linked, in that the functions pop up with colour on the compiler. When I run my code, however, literally nothing happens. There are no errors in the compiler, no comments, nothing. The window doesn't appear. I would really appreciate if anyone could help me out on the matter.
Also, SDL has worked previously on my computer before (without using SDL_image) - in which I ran a particle simulation that worked perfectly fine.
My code:
#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
using namespace std;
SDL_Window *m_window; //Window upon which the game will be displayed.
SDL_Renderer *m_renderer; //Renderer used to draw the objects on the window.
SDL_Texture *playerTex;
int SCREEN_WIDTH = 600;
int SCREEN_HEIGHT = 600;
int main(int argc, char* args[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "Video init failed" << endl;
return 1;
}
//Creates the actual SDL-window and stores it in the m_window variable.
m_window = SDL_CreateWindow("Marko Beocanin SDD Project",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_FULLSCREEN);
//Error-checking method that determines if SDL could not create a window - returns false if unsuccessful.
if (m_window == NULL) {
cout << "Window Creation failed" << endl;
SDL_Quit();
IMG_Quit();
return 2;
}
//Creates an SDL-Renderer: a tool used to actually draw objects on the Window
m_renderer = SDL_CreateRenderer(m_window, -1, 0);
//Error-checking method that determines if SDL could not create a renderer - returns false if unsuccessful.
if (m_renderer == NULL) {
cout << "Renderer creation failed." << endl;
SDL_DestroyWindow(m_window);
SDL_Quit();
IMG_Quit();
return 3;
}
SDL_Surface *tmpSurface = IMG_Load("img.png");
playerTex = SDL_CreateTextureFromSurface(m_renderer, tmpSurface);
SDL_FreeSurface(tmpSurface);
SDL_RenderClear(m_renderer);
SDL_RenderCopy(m_renderer, playerTex, NULL, NULL);
SDL_RenderPresent(m_renderer);
SDL_Delay(2000);
SDL_DestroyWindow(m_window);
SDL_Quit();
IMG_Quit();
return 0;
}
The problem I had was a result of me using the wrong SDL_image library - I was using x64 instead of x86, which meant that it didn't throw an error per se, just didn't work properly!
I am currently attempting to write a very simple 2d game engine using SDL in order to help myself get more comfortable with coding. Unfortunately, I made the mistake of starting with a single source file, and now that my code is getting bigger, I am trying to split the existing code (which works fine before the split) into multiple header and source files. The first major issue I've encountered is that when I try to define SCREEN_HEIGHT, SCREEN_WIDTH, Window, and Renderer in init.cpp after declaring them in init.h, I get error: 'blank' does not name a type (blank being Window, Renderer, etc.). I am also trying to make them global, which may be part of the issue. Included below is the relevant code (I took out all the stuff I think is irrelevant in this case). I suspect I'm just missing something really simple here, but I've been unable to find an existing answer online like I have with my previous problems.
main.cpp
#include "globals.h"
#include "texture.h"
....
globals.h
#ifndef GLOBALS
#define GLOBALS
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <stdio.h>
#include <string>
#endif //GLOBALS
texture.h
#ifndef TEXTURE
#define TEXTURE
#include "globals.h"
#include "init.h"
....
#endif // TEXTURE
texture.cpp
#include "texture.h"
....
init.h
#ifndef INIT
#define INIT
#include "globals.h"
//screen dimensions
int SCREEN_WIDTH;
int SCREEN_HEIGHT;
//initiates SDL and creates a window and renderer
bool init();
//the created window
SDL_Window* Window;
//the renderer that will be used
SDL_Renderer* Renderer;
#endif // INIT
init.cpp
#include "init.h"
SCREEN_WIDTH = 640;
SCREEN_HEIGHT = 480;
Window = NULL;
Renderer = NULL;
bool init()
{
bool success = true;
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
{
printf( "SDL was unable to initialize! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf( "Linear texture filtering not enabled!");
}
else
{
Window = SDL_CreateWindow( "Window", 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());
success = false;
}
else
{
Renderer = SDL_CreateRenderer( Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if( Renderer == NULL)
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
SDL_SetRenderDrawColor( Renderer, 0xFF, 0xFF, 0xFF, 0xFF);
int PNGImage = IMG_INIT_PNG;
if( !(IMG_Init( PNGImage) & PNGImage))
{
printf( "Image loading not enabled! SDL_Image Error: %s\n", IMG_GetError());
success = false;
}
if( TTF_Init() == -1)
{
printf( "True type fonts not enabled! SDL_TTF Error: %s\n", TTF_GetError());
success = false;
}
}
}
}
}
return success;
}
I get the error in init.cpp at the top of the code before bool init()
Renderer = NULL; is an assignment statement, which you cannot have in global scope. You can only have declarations in global scope. Since you have already declared Renderer in your header file, you don't need to redeclare it either. Just set it to null within one of your initialization functions (e.g. your current init() function).
I'm trying to run this code but it keeps giving me an error.
I copyed SDL2_image.lib in the debug folder but in vain.
I'm at the beggining of programming so please be patient.
Errors:
Error 1 error C3861: 'IMG_LoadTexture': identifier not found
Error 2 IntelliSense: identifier "IMG_LoadTexture" is undefined
#include<SDL/SDL.h>
#include<iostream>
using namespace std;
int main(int argc, char** argv)
{
bool quit = false;
//*Initializing Window;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = NULL;
window = SDL_CreateWindow("Game Test", 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
//*If game Crushes;
if (window == NULL)
{
cout << "The game window is not working";
}
//*Creating Update Function
SDL_Renderer* render = NULL;
render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event* mainEvent = new SDL_Event();
//*End Update Function
//*Adding Textures;
SDL_Texture* grass_image = NULL;
grass_image = IMG_LoadTexture(render, "grass.bmp");
//*Creating a Sprite;
SDL_Rect grass_rect;
grass_rect.x = 10;
grass_rect.y = 50;
grass_rect.w = 250;
grass_rect.h = 250;
//*Content Of the Window;
while (!quit && mainEvent->type!=SDL_QUIT)
{
SDL_PollEvent(mainEvent);
SDL_RenderClear(render);
SDL_RenderCopy(render, grass_image, NULL, &grass_rect);
SDL_RenderPresent(render);
}
//*End Window Content
//*Memory Cleaning
SDL_DestroyWindow(window);
SDL_DestroyRenderer(render);
delete mainEvent;
//*End Memory Cleaning
return 0;
}
You are missing to include the header that contains the declaration of IMG_LoadTexture():
#include <SDL/SDL_image.h>
It's a separate extension library for SDL, and besides including that header, you'll also need to link that library with your project.
I have started to make a game in C++ with SDL. My Code is:
main.cpp
#include "SDL/SDL.h"
#include "gameSystem.h"
int main(int argc, char *args[])
{
gameSystem systemHandler;
SDL_Surface *buffer;
SDL_Event event;
while(event.type != SDL_QUIT)
{
SDL_PollEvent(&event);
SLD_Flip(buffer);
}
}
gameSystem.cpp
#include "SDL/SDL.h"
#include "gameSystem.h"
gameSystem::gameSystem()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption("GameName", NULL);
SDL_Surface *buffer;
bool fullscreen = false;
if (fullscreen == true)
{buffer = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE | SDL_FULLSCREEN);}
else
{buffer = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);}
}
gameSystem::~gameSystem()
{
SDL_Quit();
}
gameSystem.h
class gameSystem
{
public:
gameSystem();
~gameSystem();
private:
SDL_Surface *buffer;
};
My linker options include: -lmingw32 -lSDLmain -lSDL -lwinmm -sdl_Mixer
I get the following error: "error: 'SLD_Flip' was not declared in this scope" on line 12 of main.cpp.
All the other SDL functions seem to be fine.
Does anyone know how to fix this problem?
You misspelled it. It should be SDL_Flip, not SLD_Flip.
Also, you haven't initialized your variable buffer in main. You are not setting its value inside of gameSystem::gameSystem(); the variable is out of scope.