I am working on a little project in C++ with SDL2. I am trying to create a global window which I can use in my other .cpp files, but I cant figure out how to make a global variable in SDL. And please don't write something like "Don't use global variables", because I have to use them, otherwise it won't work.
In every file you need it just declare it as extern SDL_Window* GWindow; and use it; then in single .cpp file define it SDL_Window* GWindow = nullptr;
Related
I'm working on a SFML project that contains several different files and classes. The problem is : in every file, there is some drawing, and the sf::RenderWindow* defined in the main file seems to be out of scope here, therefore I can't link these drawing with the main window. I've tried to redefine sf::RenderWindow in every header files, and it doesn't work as well (a black screen appear and disappear promptly). The error was : Access violation reading location. But when I group everything in one file, things work well but it looks really messy.
I also found this question asked on another forum, and the answers were "using reference and external method", which is a bit vague to me. Any help on this linking problem would be appreciated :(
I think that answer meant something like:
void draw_in_class (sf::RenderWindow* window){
window->draw(...);
}
in class, and then you can use 'draw_in_class' in main:
#include"class.h"
... // Define sf::RenderWindow here
draw_in_class(&window);
I am designing an immediate gui using C++ and SDL2. As I was starting to design my framework, I noticed that basically everything needs a renderer or some sort of event or other relevant information (Fonts, Themes, etc.). Does it make sense to put these is global static object that only the gui functions have access to, or just keep passing everything in.
Would this be ok?
This could be the header:
struct GuiForm {
GuiId active_id;
GuiId hot_id;
std::vector<Window> windows;
Renderer* renderer;
};
Then this could be the .cpp file:
static GuiForm* gui_form;
void SomeGuiWidget() {
GuiId current_hot_id = gui_form.hot_id;
/* Whatever the widget is going to do */
}
I'm trying to use ChaiScript with SFML for my game engine. All the SFML stuff works fine, until I include chaiscript.hpp in my game object header file. My GameObject header file looks a little like this:
#include <a bunch of standard libraries>
#include "imgui.h"
struct Object {
std::string name;
void init();
void update();
void render();
*some template functions*
};
The problem occurs when I try to include chaiscript.hpp in the above file. I get a repeating error (8 of them, to be exact) from SFML's Rect.inl file: '(': illegal token on right side of '::', and then no other errors. It compiles fine when I don't include chaiscript.hpp, or when I include chaiscript.hpp in the object cpp file. I've used ChaiScript before with SDL and never had an issue like this, so is it an SFML macro messing something up? How can I avoid this problem?
It sounds like something in ChaiScript is messing up something in SFML. Reversing the order that you include them may remove those errors (include SFML before ChaiScript, or vice versa).
I have problem with display image (I tried to rewrite engine from SDL to SFML 2.0; engine downloaded from:
http://gamedevgeek.com/tutorials/managing-game-states-in-c/
)
I have problem with especially that part of code that is in introstate.cpp.
The program compiles and create window (just for one sec) and then vanishes with no reaction and no render anything (it should display image).
I think it has to do with range of object sf::RenderWindow MarioClone. I mean it was declared in few headers and used in variety methods, so I think there's misunderstanding with pointing to the specific window that is created. Should I use "extern" keyword somwhere or what?
I leave link to github because code is in many files and even one file contains a lot of code and don't want to paste it here (it would be hard to read).
https://github.com/shahar23/MarioClone
(And yes - the code has previous original SDL commented to understand easily what should be put in methods instead)
In your gameengine.cpp file, in your init method, you create a local variable of the same name as the variable declared in your header file. That's not what you want. You want to change the existing variable:
void CGameEngine::Init(const char* title, int width, int height, bool fullscreen)
{
// This line creates a NEW LOCAL variable of the same name.
// Your instance level variable remains unchanged:
// sf::RenderWindow MarioClone(sf::VideoMode(width, height), title, sf::Style::Default);
// instead, change your class level variable:
MarioClone.create(sf::VideoMode(width, height), title, sf::Style::Default);
I have a variable in a main.cpp file like this:
SDL_Renderer* gRenderer = NULL;
and I have a class that is in separate files (a .h and a .cpp file).
Inside the .cpp file I want to access gRenderer like this:
newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
I have tried putting an SDL_Renderer inside the class but when I compile it gives me only one warning that it is unused and when I run the program I get a message from SDL_GetError() :
"Unable to create texture from colors.png! SDL Error: Invalid
renderer"
How can I do that inside the class that is in the separate files?
In order to access it, its file must be included in the file it will be used. Therefore, you should move the variable declaration to a header file (like main.h) and include it in main.ccp and in the files it will be used.
To do so, though, in the header file you should declare it as extern, and in main.cpp, defined normally:
main.h
extern SDL_Renderer* gRenderer;
main.cpp
SDL_Renderer* gRenderer = NULL;
That way, the variable is defined and can be used across multiple files normally, retaining its value.
If you want more information:
How do I use extern to share variables between source files?