First question here, and answer is probably very simple, but i can't figure it out. To the point:
In my project i created 2 classes: "GlobalVairables" and "SDLFunctions".
Obviously, in first one i want to store globals which i could relate to in any other class, and in the second on i got few functions using those globals. Here's code:
GlobalVariables.h
#pragma once
class GlobalVariables
{
public:
GlobalVariables(void);
~GlobalVariables(void);
const int SCREEN_WIDTH;
const int SCREEN_HEIGHT;
//The window we'll be rendering to
SDL_Window* gWindow;
//The surface contained by the window
SDL_Surface* gScreenSurface;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld;
};
and GlobalVariables.cpp
#include "GlobalVariables.h"
GlobalVariables::GlobalVariables(void)
{
const int GlobalVairables::SCREEN_WIDTH = 640;
const int GlobalVariables::SCREEN_HEIGHT = 480;
SDL_Window GlobalVairables:: gWindow = NULL;
SDL_Surface GlobalVariables:: gScreenSurface = NULL;
SDL_Surface GlobalVariables:: gHelloWorld = NULL;
}
GlobalVariables::~GlobalVariables(void)
{
}
and here is one function in SDLFunction.cpp, that uses "gWindow" and 2 other variables:
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
My problem is, that on debugging, i get
error C2065: 'gWindow' : undeclared indentifier
Of course, in SDLFunctions.cpp i got "#include "GlobalVariables.h" ". Also, those variables are public, so it's not this (probably).
Can someone tell what's wrong? Is there some simple solution, or do i have to reorganize it, and shouldn't use globals? Please help.
First of all, your variables are members of every instance of the class, and as such, are not globals in usual meaning. You might want to declare them static. What is even better, do not create a class for them at all - instead, put them into namespace. Something like following (in your .h file):
namespace globals {
static const unsigned int SCREEN_WIDTH = 640;
static const unsigned int SCREEN_HEIGHT = 1024;
}
Than you can reference them in your code in a following manner:
int dot = globals::SCREEN_WIDTH;
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'm new and still learning OOP and SDL for educational purpose.
so, i have a variable SDL_Renderer renderer. this variable needs to be initiated only once, and i initiate it in GameManager class.
and i have another class named Texture that needs that renderer.
this Texture will be used frequently.
so how do i pass this renderer? do i have to call GameManager in the Texture class? but if i do that, it means that i makeGameManager everytime i use the Texture right? or there is another way around?
thank you for helping me, i'm really sorry if my question is vague or not clear.
EDIT
this is Texture class
class Texture
{
public:
Texture();
~Texture();
int getWidth();
int getHeight();
bool loadFromFile(std::string path);
bool loadTextFromFile(std::string text, SDL_Color textColor, TTF_Font* font);
void render(int x, int y, SDL_Rect* clip = NULL);
void free();
bool lockTexture();
bool unlockTexture();
void* getPixels();
int getPitch();
private:
int vWidth;
int vHeight;
SDL_Texture* vTexture;
SDL_Renderer* renderer;
void* pPixels;
int pPitch;
};
this is the initiator
Texture::Texture()
{
vTexture = NULL;
vWidth = 0;
vHeight = 0;
renderer = GameManager::getRenderer();
}
this is GameManager class
class GameManager
{
public:
GameManager();
~GameManager();
bool intializeGame();
void gameLoop();
static SDL_Renderer* getRenderer();
private:
SDL_Window* window = NULL;
static SDL_Renderer* renderer;
TTF_Font* font = NULL;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
};
the getRenderer() just a getter to pass the renderer
and this is my Main
int main(int argc, char* args[])
{
GameManager gameManager;
gameManager.intializeGame();
Texture charTexture;
SDL_Rect rect;
bool text = charTexture.loadFromFile("foo.png");
if (!text)
{
printf("texture not loaded");
}
rect.x = 0;
rect.y = 0;
rect.w = charTexture.getWidth();
rect.h = charTexture.getHeight();
while (true)
{
SDL_SetRenderDrawColor(GameManager::getRenderer(), 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(GameManager::getRenderer());
charTexture.render(10, 10, &rect);
SDL_RenderPresent(GameManager::getRenderer());
}
return 0;
}
i hope it's not confusing.
Disclaimer : I've never used SDL. This might be terrible, but it's based on what you gave me.
The important thing is the ownership. This is a shared ownership example. It's quite simple and takes the burden of figuring out when to destroy SDL_Renderer off of you.
class GameManager {
//BLABGLABLA
public:
std::shared_ptr<SDL_Renderer> getRenderer();
private:
std::shared_ptr<SDL_Renderer> renderer
}
class Texture
{
public:
Texture(std::shared_ptr<SDL_Renderer> theRenderer, //some other args)
private:
std::shared_ptr<SDL_Renderer> renderer;
}
So based on the name of the classes alone, you probably want GameManager to own the renderer, but you also want Texture to have access to it.
One way you can do that is to have a shared_ptr member in both classes, and to pass the renderer to texture in its constructor.
Basically the renderer object you initialized in GameManager, will only be destroyed when the last shared_ptr pointing to it is destroyed.
I'm fairly new to programming, so this question will probably be basic. I'm writing a very basic program in C++ with the SDL2 library (in Visual Studio 2013). When I was writing it, I came across a problem. I wrote the following:
int controles(){
//declare actions that will happen when a key is pressed
const Uint8 * estado = SDL_GetKeyboardState(NULL);
if (estado[SDL_SCANCODE_UP]){ y--; SDL_UpdateWindowSurface(ventana); }
if (estado[SDL_SCANCODE_DOWN]){ y++; SDL_UpdateWindowSurface(ventana); }
return 0;
}
The problem is that I need to update the window surface after the value of y is modified, but I get an error because ventana, the name of the window, is defined in another function. I tried to define ventana globally, but the program won't work then. I then thought the following; write a goto statement in graficos, the function where ventana is defined, in order to skip every other statement in that function, except for the one that updates the window surface. However, when I did that, the program doesn't even compile:
int graficos(int caso){
if (caso == 1) {goto reload;} //skip to reload if (1)
SDL_Init(SDL_INIT_VIDEO); //load SDL
//load graphics in memory
SDL_Window * ventana = SDL_CreateWindow("ventana", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
SDL_Surface * superficie = SDL_GetWindowSurface(ventana);
SDL_Surface * pantallainicio = SDL_LoadBMP("pantallainicio.bmp");
SDL_Surface * paleta = SDL_LoadBMP("paleta.bmp");
SDL_Rect rpantallainicio = { 0, 0, 640, 480 };
SDL_Rect rpaleta = { x, y, 16, 16 };
//render graphics
SDL_BlitSurface(pantallainicio, NULL, superficie, &rpantallainicio);
SDL_BlitSurface(paleta, NULL, superficie, &rpaleta);
SDL_UpdateWindowSurface(ventana);
reload:SDL_UpdateWindowSurface(ventana);
return 0;
}
I get the following errors:
error C4533: initialization of 'rpaleta' is skipped by 'goto reload'
error C4533: initialization of 'rpantallainicio' is skipped by 'goto reload'
I hope I explained my issue well enough. What can I do? Is there a way to fix this? Or can I reference ventana in some other way? This issue might be very basic, sorry for that, and thanks in advance!
You can fix this issue by simply not using goto at all - use a sub-function instead. Also, extract the variable ventana, as you need it to be stored and usable by graficos whenever.
void init()
{
SDL_Init(SDL_INIT_VIDEO); //load SDL
//load graphics in memory
ventana = SDL_CreateWindow("ventana", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
SDL_Surface * superficie = SDL_GetWindowSurface(ventana);
SDL_Surface * pantallainicio = SDL_LoadBMP("pantallainicio.bmp");
SDL_Surface * paleta = SDL_LoadBMP("paleta.bmp");
SDL_Rect rpantallainicio = { 0, 0, 640, 480 };
SDL_Rect rpaleta = { x, y, 16, 16 };
//render graphics
SDL_BlitSurface(pantallainicio, NULL, superficie, &rpantallainicio);
SDL_BlitSurface(paleta, NULL, superficie, &rpaleta);
}
int graficos(int caso)
{
if (caso != 1) { init(); } //skip to reload if (1)
SDL_UpdateWindowSurface(ventana);
return 0;
}
SDL_Window * ventana;
Use of goto should generally be avoided. Use subroutines or other alternatives where possible. Here, the code does exactly the same thing as originally intended, but the "extra" flow that occurs when caso is not 1 is wrapped in its own subroutine named 'init'.
I've been trying to improve my C++ skills and explore different ways of programming in it by using libraries like SDL.
This particular program is a Particle system and i've followed a guide on youtube that can be found here: http://www.youtube.com/watch?v=6Z5FI5180oo&list=UU6A2B9G_y-fzAXEu2hHPlMg.
The problem is that this guide is using SDL 1.2 and i've been trying to translate the depricated functions used in here to SDL 2.0.
Basically it works fine apart from some functions where i try to retrieve the width/height/pixels/pitch from the Windowsurface. SDL 1.2 uses the function GetVideoSurface() whereas i do believe SDL2.0 should use GetWindowSurface().
I'm having problems with accessing this function outside of my main function and i'm not sure what the problem really is.
For example in my particle::show() function i have this code:
void particle::show()
{
Uint8* pixels=(Uint8*)SDL_GetWindowSurface(window)->pixels;
Uint8* pixel=pixels+(int)y*SDL_GetWindowSurface(window)->pitch+(int)x;
*pixel=color;
}
Here i can't access these functions, i get no error message. Even if i try to use SDL_GetError(). The program crashes with a segmentation fault and no other errors (that i can see.)
This is my main function:
int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow("My Particles",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640, 480, SDL_WINDOW_OPENGL);
bool running = true;
const int FPS = 30;
Uint32 start;
srand(time(0));
particleEngine ps (100, SDL_GetWindowSurface(window)->w/2, SDL_GetWindowSurface(window)->h/2);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
while(running)
{
start = SDL_GetTicks();
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running = false;
break;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
ps.refresh();
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
SDL_Quit();
return 0;
}
I've defined my SDL_Window *window outside of every function as a global variable.
I'm running SDL 2.0 and Mac OS X 10.10 Yosemite Beta 2
Any ideas how i should solve this?
SDL_Window *window is a local variable in the int main(int argc, char** argv) function. It will not be visible outside of main unless you either make it a global variable (not nice) or make it a member of a class which you create an instance of.
Alternatively you could pass the pointer to the SDL_Window to the functions which you need it in.
I noticed you said you have declared it as a global variable and in that case that is why it compiles but doesn't work. The variable used in the particle::show() will be using the global variable which will points to nothing (globals are always zero-initialized by default). As you declare SDL_Window again in main() that is what gets assigned the created window.
My code:
window.cpp
Window::Window(int w, int h, const char *title, const char *icon)
{
height = h;
width = w;
if(SDL_Init( SDL_INIT_EVERYTHING ) == 0)
{
SDL_WM_SetCaption(title, NULL);
SDL_WM_SetIcon(SDL_LoadBMP(icon),NULL);
screen = SDL_SetVideoMode(width, height, 32,
SDL_SWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
if(screen == NULL)
{
running = false;
return;
}
fullscreen = false;
}
else
running = false;
return;
}
Window::Window()
{
const SDL_VideoInfo* info = SDL_GetVideoInfo();
screenWidth = info->current_w;
screenHeight = info->current_h;
Window(640, 480, "Flatgu game", "rsc/img/icon.bmp");
}
window.h
class Window
{
public:
Window();
~Window();
int getWidth() {return width;}
int getHeight() {return height;}
bool isFullscreen() {return fullscreen;}
void toggleFullscreen();
private:
Window(int w, int h, const char *title, const char *icon);
bool fullscreen, running;
int height, width, screenWidth, screenHeight;
SDL_Surface *screen;
};
It compiles fine, but then, after compiling, I'm getting this ugly error:
What's the reason of my problem? Why do I get so weird numbers?
My aim is to store original screen resolution for further use (like toggling to fullscreen), and I have to do this before calling SDL_SetVideoMode(). That's why it is in the constructor.
You have a problem with calling SDL Video Functions before actually initializing SDL.
SDL_Init( SDL_INIT_EVERYTHING )
has to be called before
SDL_GetVideoInfo();
In your case you call SDL_GetVideoInfo(); first
const SDL_VideoInfo* info = SDL_GetVideoInfo(); //<-- calls SDL_GetVideoInfo();
screenWidth = info->current_w;
screenHeight = info->current_h;
Window(640, 480, "Flatgu game", "rsc/img/icon.bmp"); //<-- initializes SDL
So the solution is simple; make the call SDL_Init( SDL_INIT_EVERYTHING ) immediately at the start of your program, then you can call SDL_GetVideoInfo(); as much as you like.
You will have to restructure your class Window slightly.
To get the best video mode call SDL_GetVideoInfo before setting up the video (before calling SDL_SetVideoMode).
But you still have to initialize the video subsystem before calling it (SDL_Init(SDL_INIT_VIDEO)).
I know this is old, but there's a big mistake in the code.
Window(640, 480, "Flatgu game", "rsc/img/icon.bmp");
creates a nameless instance of a Window, so the instance that calls it will still have uninitialized variables. It looks like you were trying to use delegating constructors, but in that case the call to the other constructor must be in the member initializer list.
See this page.