I'm trying to create a game window for a cave story clone in C++, so first, I create the header file below, after that, I went to create the class file. When I finish the class I kept receiving the error that the argument type is incomplete with a parameter of type sdl_window and sdl_render. If anyone could help me figure out what I'm doing wrong.
Graphics.h
#ifndef GRAPHICS.h
#define GRAPHICS.h
struct SDL_window;
struct SDL_render;
class Graphics {
public:
Graphics();
~Graphics();
private:
SDL_window* window = NULL;
SDL_render* render = NULL;
};
#endif
Graphics.cpp
#include <SDL.h>
#include "graphics.h"
/* Graphics class
* Holds all information dealing with graphics for the game
*/
Graphics::Graphics() {
SDL_CreateWindowAndRenderer(640, 480, 0, &window, &render);
SDL_SetWindowTitle(window, "Cavestory");
}
Graphics::~Graphics() {
SDL_DestroyWindow(window);
}
The problem is that you are declaring your own types unrelated to SDL types. Rewrite class to use appropriate types:
#include <SDL.h>
class Graphics {
public:
Graphics();
~Graphics();
private:
SDL_Window * window = nullptr;
SDL_Renderer * render = nullptr;
};
Related
I have two namespaces with each a pointer to a class Window inside of one of the namespaces.
Graphic.h
namespace Graphic {
//...
class Window;
//...
void init();
static Window* window;
}
App.h
namespace App{
//...
static Graphic::Window* mainWindow = nullptr;
//...
void init();
void setupGraphic();
//...
}
I am trying to initialize the static Window* App::mainWindow with the static Window* Graphic::window
Window.h
#include "Graphic.h"
#include <SDL\SDL.h>
class Graphic::Window
{
public:
Window(const char* pName = "OpenGL Window",
unsigned int pWidth = 1000, unsigned int pHeight = 700);
~Window();
const char* name;
unsigned int width;
unsigned int height;
SDL_Window* window;
};
The Graphic::window is being initialized like so
Graphic.cpp
#include "Graphic.h"
void Graphic::init(){
window = new Window("Engine");
}
And after this initialization i try to initialize the (static Window*)App::mainWindow with Graphic::window
App.cpp
#include "App.h"
#include <Graphic\Graphic.h>
#include <Graphic\Window.h>
void App::setupGraphic()
{
Graphic::init();
App::mainWindow = Graphic::window;
}
But App::mainWindow stays nullptr, even though Graphic::window has successfully been initialized and has already been worked with in Graphic::init(). There are no compilation warnings/errors, all i get is an exception "App::mainWindow was nullptr."
In "Graphic.h", you have
static Window* window;
This statement is included in every translation unit (.cpp) that will #include Graphic.h. Therefore each unit will have its own variable window. What happens then is that Graphic.cpp assigns its own window, but main.cpp find its own variable window unchanged.
What you should do is the following:
In Graphic.h, declare window but don't define it:
extern Window* window;
And define it only once, in Graphic.cpp:
Window* Graphic::window = nullptr;
This way all the translation units will refer to the same global variable window.
You should do the same for the variable Graphic::Window* mainWindow defined in App.h.
extern Graphic::Window* mainWindow; // <-- in App.h
And
Graphic::Window* App::mainWindow = nullptr; // <-- in App.cpp
I got following problem:
I got my main c++ file, in which i have included
#include <SDL2/SDL.h>
After having written some SDL c++ code, then i want to split up my program into different classes.
the problem is that i try to say:
#include <SDL2/SDL.h>
in the new "Engine" class but i doesn't seem like it is including the SDL.
Im using xcode 5.
The SDL frameworks works fine if i write the code in my main.cpp
Engine class:
#include "Engine.h"
#include <SDL2/SDL.h>
using namespace std;
class Engine
{
SDL_Window *window = NULL;
SDL_Surface *screenSurface = NULL;
public:
Engine();
bool init();
bool loadMedia();
void close();
}
Im still on the drawing board with what kind of classes i need.
This is Engine.h
#ifndef __Engine4__Engine__
#define __Engine4__Engine__
#include <iostream>
#include <SDL2/SDL.h>
class Engine
{
}
#endif /* defined(__Engine4__Engine__) */
My xcode5 wont come with suggestions when i write SDL_
The issue is that you have not created Engine.h and are trying to reference it when it doesnt exist. The issue is not with SDL, rather that you are unsure how to create a class in c++.
You need to create both Engine.h and Engine.cpp
Engine.h will look something like
#ifndef ENGINE_H
#define ENGINE_H
#include <SDL2/SDL.h>
class Engine
{
public:
Engine();
bool init();
bool loadMedia();
void close();
private:
SDL_Window *window;
SDL_Surface *screenSurface;
};
#endif
and then you need to create a Engine.cpp file that will look like
#include "Engine.h"
Engine::Engine() :
window(nullptr),
screenSurface(nullptr)
{
}
// Rest of code from header file
See more about creating classes in C++ here.
Im trying to make a game with the SFML.
I'm making a sf::RenderWindow but when I try to pass the window to another class
it fails. I can't access the window. Because I think it's good to make a separate
class for handling events like 'close the window' etc. but then I can't access it. How
can I fix this?
RenderWindow *window;
window = new RenderWindow(VideoMode(768, 614), "Tower Defence ver 2.0");
Create yourself a header file and define your function like so
Header file
#pragma once
#include "SFML/Graphics.hpp"
class MyClass
{
public:
sf::Sprite Sprite;
MyClass();
void Setup(sf::Texture& texture);
void Draw(sf::RenderWindow& window);
};
Cpp file
#include "Bullet.h"
MyClass::MyClass()
{
}
void MyClass::Setup(sf::Texture& texture)
{
Sprite.setTexture(texture);
Sprite.setPosition(0, 0);
}
void MyClass::Draw(sf::RenderWindow& window)
{
window.draw(Sprite);
}
Then in your game loop for drawing you can call something like this
// myClass is an object of type MyClass
// renderWindow is your sf::RenderWindow object
myClass.Draw(renderWindow);
Hope this helps. Let me know if you need any more guidance.
RenderWindow is in a namespace 'sf'
Maybe you have somewhere "using namespace sf;" and it's missing in other places.
Try prefixing it with sf::RenderWindow everywhere.
Which version of SFML are you using? This is not possible in SFML 1.6, but is in SFML 2.0 (upcoming version).
try this
class Foo
{
public:
Foo(sf::RenderWindow& ptrwindow)
: ptrwindow(ptrwindow)
{
// your code here
};
sf::RenderWindow* window()
{
return &this->ptrwindow;
}
private:
sf::RenderWindow ptrwindow;
};
int main()
{
sf::RenderWindow* mywindow = new sf::RenderWindow()
Foo myfoo(*mywindow);
myfoo.window()->create(sf::VideoMode(768, 614), "Tower Defence ver 2.0")
}
I'm having troubles with C++.
I am making an Engine class for my game that handles graphic using SDL.
The Engine class is (hopefully correctly implemented) Singleton.
engine.h:
#ifndef H_ENGINE
#define H_ENGINE
#ifndef H_SDL
#include "SDL/SDL.h"
#endif
class Engine {
public:
static Engine *getInstance(); //This returns the singleton object of class
int init(int screenWidth, int screenHeight); //must initialize before use
~Engine(); //destructor
private:
Engine(); //private constructor
static Engine *instance; //stores the single instance of the class
SDL_Surface *screen; //Struct for SDL
};
#endif
engine.cpp:
#include "engine.h"
Engine *Engine::instance = NULL;
Engine::Engine() {
screen = NULL;
}
Engine *Engine::getInstance() {
if(instance == NULL)
instance = new Engine();
return instance;
}
int init(int screenWidth, int screenHeight) {
SDL_Init(SDL_INIT_EVERYTHING);
//This line has the error: error: ‘screen’ was not declared in this scope
screen = SDL_SetVideoMode(screenWidth, screenHeight, 32, SDL_SWSURFACE);
return 1;
}
Engine::~Engine() {
SDL_Quit();
}
main.cpp: contains the line
Engine::getInstance()->init(600, 400);
Any help would be appreciated
You forgot to put the class qualifier on init:
int Engine::init(int screenWidth, int screenHeight)
Happens all the time.
You define init as:
int init(int screenWidth, int screenHeight) {
However, this defines a function in global scope (there there is no screen variable).
Instead, if you write:
int Engine::init(int screenWidth, int screenHeight) {
You will define the function of your class.
I'm learning gtkmm in order to program Conway's Game of Life as a demo. Currently I'm trying to show two buttons in a header bar, and I'm following a tutorial, but nothing is showing up in the window. Here's my code:
Display.h:
#include <gtkmm/window.h>
#include <gtkmm/headerbar.h>
#include <gtkmm/button.h>
class Display : public Gtk::Window
{
public:
Display();
Display(int xSize, int ySize);
virtual ~Display();
private:
//child widgets
Gtk::HeaderBar mHeader;
Gtk::Button startButton;
Gtk::Button stopButton;
};
Display.cpp:
#include "Display.h"
Display::Display(int xSize, int ySize):
startButton("start"),
stopButton("stop"),
mHeader()
{
//set window properties
set_title("Conway's Game of Life");
set_size_request(xSize, ySize);
set_border_width(5);
mHeader.set_title("Game of Life");
//add to header bar
mHeader.pack_start(startButton);
mHeader.pack_start(stopButton);
//add header bar
add(mHeader);
//make everything visible
show_all();
}
Display::Display()
{
Display(600, 600);
}
Display::~Display() {}
Main.cpp:
#include "Display.h"
#include <gtkmm.h>
int main(int argc, char **argv)
{
auto app = Gtk::Application::create(argc, argv);
Display Window;
return app->run(Window);
}
I've been trying to fix this for quite a while and can't seem to figure it out. Any help would be greatly appreciated.
The problem is that you are not using constructor delegation correctly. Try to write your default constructor as follow instead:
Display::Display()
: Display(600, 600) // Delegate here, not in body...
{
}
and it should work. Note that this is a C++11 feature.