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?
Related
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).
ALL,
The following code:
in fontpropertypage.cpp:
#import "AppKit/AppKit.h"
CFontPropertyPage::CFontPropertyPage()
{
m_font = font;
NSFontManager *manager = [NSFontManager sharedFontManager];
NSFontPanel *panel = [manager fontpanel:true];
}
In the fontpropertypage.mm
#include "fontpropertypage.cpp"
I'm receiving the error in subject from the Xcode when trying to get NSFontPanel pointer.
Can someone please help with the fix?
Both files are present in the Xcode project.
TIA!!
NSFontManager and NSFontPanel are GUI objects in Cocoa and can only be accessed with Objective-C (or Objective C++, your .mm file).
This code would surely compile if you put it directly in the .mm file, but having it in a .cpp file that you're including is wierd (I usually only ever include header files like .h or .hpp).
I suggest putting that class directly into the .mm file.
Also, the code you have up there has a few errors in general:
CFontPropertyPage::CFontPropertyPage()
{
// m_font should be a class member ; what about font?
m_font = font;
NSFontManager *manager = [NSFontManager sharedFontManager];
// fontPanel needs a capital P
NSFontPanel *panel = [manager fontPanel:true];
}
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 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;
someone passed me an openFrameworks project src folder. The project has a class called "rectangle."
Inside the src folder there are main.h, testApp.cpp, testApp.h, rectangle.cpp, rectangle.h.
I directly replace the src folder in my empty project, but the project won't compile.
I believe the search directories are right since the class files are also in the src folder.
However CB shows me the "error: Rectangle does not name a type" in testApp.h
Rectangle myRect;
I already included the header files in testApp.h (#include "rectangle.h") and have the rectangle.h include ofMain.h
Not sure if I am doing the right way to include a class.
Thanks!
The testApp.h file:
The rectangle.h file:
The rectangle cpp:
file structure:
You don't give enough information so it is all speculation. However, my guess is that the barely visible ofBa... is the culprit! If this class happens to have a function called Rectangle, this names is found and it clearly isn't a type. Here is the short example you should have posted, demonstrating the problem:
class Rectangle
{
};
struct foo
{
void Rectangle() {}
};
struct bar
: foo
{
Rectangle r;
};
One fix for the problem is to use ::Rectangle instead of Rectangle as this qualification forces the look-up at global scope.