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")
}
Related
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;
};
I'm trying to make a framework for an SFML game I'm making and I'm having trouble with inheritance. I thought I knew how to do it but it doesn't seem right. I want my main to just call GameLoop basically and thats it. In my GameLoop class I have methods for making the window and running the game. Then I want my GamePlayScreen class to actually handle the game logic but for some reason I can't figure out how to inherit from GameLoop. I was watching a video and the guy was using C# which is why I'm having trouble converting it. The video is https://www.youtube.com/watch?v=WhbeqOOSDEo&index=2&list=PLfTDIoEcaNroztBVGPA0aU3NbOauRVIe3.
GameLoop.h
#pragma once
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <string>
class GameLoop
{
public:
GameLoop(int width, int height, std::string title);
virtual void Run();
virtual void LoadContent();
virtual void Initialize();
virtual void Update();
virtual void Render();
sf::RenderWindow window;
};
GameLoop.cpp
#include "GameLoop.h"
GameLoop::GameLoop(int width, int height, std::string title)
{
window = sf::RenderWindow(sf::VideoMode(width, height), title, sf::Style::Default);
}
void GameLoop::Run()
{
LoadContent();
Initialize();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
Update();
window.clear();
Render();
window.display();
}
}
void GameLoop::LoadContent()
{
}
void GameLoop::Initialize()
{
}
void GameLoop::Update()
{
}
void GameLoop::Render()
{
}
GamePlayScreen.h
#pragma once
#include "GameLoop.h"
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
class GamePlayScreen : public GameLoop
{
public:
GamePlayScreen();
void Initialize();
};
GamePlayScreen.cpp
#include "GamePlayScreen.h"
GamePlayScreen::GameLoop(800, 600, "Game");
{
}
void GamePlayScreen::Initialize()
{
GameLoop game(800, 600, "Game");
}
The constructor will be called automatically when you 'new' the class. Just make a matching constructor in GamePlayScreen and the system will call the derived constructor first - but the signature must match.
class GamePlayScreen : public GameLoop
{
public:
GamePlayScreen();
GamePlayScreen(int width, int height, std::string title);
void Initialize();
};
Maybe this is not a real answer but I can not comment.
As I can see main subject is about "Handling Multiple Screens".
So you may look at this tutorial: https://github.com/SFML/SFML/wiki/Tutorial:-Manage-different-Screens
This question already has answers here:
Circular C++ Header Includes
(6 answers)
Closed 9 years ago.
So I am making a game trying to use OOP but visual studio isn't happy I am using VC++ 2010 express as my IDE, I decided to "Objectify" SDL into reusable components but now I went from 0 errors to 122 and some of them really aren't errors like when it says window is not defined and i click to go to the error the error disappears... so I am having a bit of trouble
#ifndef GUARD_WINDOWSTATE_H
#define GUARD_WINDOWSTATE_H
#include<SDL.h>
#include"Window.h"
#include"Graphics.h"
/*
WindowSate is a component to a window class, a windowstate is a seperate
frame of a window, it could be a menu or a level or something
a windowstate handles its events through a window and draws via graphics buffer to
the window's surface it will hold the components of the window(graphics, GUI components, etc.)
and the window will hold it
*/
class WindowState {
public:
WindowState(Window* window)
:m_window(window)
{
m_window->add_state(this);
}
virtual void load() = 0;
virtual void update( SDL_Event* e) = 0;
virtual void logic() = 0;
virtual void render(Graphics g) = 0;
virtual void unload() = 0;
protected:
Window* m_window;
};
#endif
it tells me that window is undefined when it is defined in window.h which is included
here is window.h:
#ifndef GUARD_WINDOW_H
#define GUARD_WINDOW_H
#include"SDL.h"
#include<vector>
#include<string>
#include"WindowState.h"
#include"Graphics.h"
/*The window class is the interface to the user,
it displays everything and is the top layer of the interactive application
it recieves events and passes them down the chain to states and components
it holds states and enumerates through them
*/
class Window {
private:
//width and height of the window
int m_width, m_height;
//graphics handler of the window
Graphics m_g;
//window surface
SDL_Surface* m_surface;
//event to hold all events
SDL_Event m_events;
//current and qeued states
int m_current_state;
int m_queued_state;
//Current WindowState
WindowState* m_window_state;
//is the window open?
bool m_running;
//title of the window
std::string m_title;
//vector to hold all the window states
std::vector<WindowState*> m_states;
public:
//Basic constructor
Window(int width, int height);
//constructor with title
Window(int width, int height, std::string title);
//returns the windows events
virtual SDL_Event& get_events() { return m_events; }
SDL_Surface* surface() { return m_surface; }
bool is_running() { return m_running; }
virtual void run(); //starts the window
virtual void update(); //updates the events
virtual void update_state(); //checks for a change in state
virtual void draw(); //draws current state
virtual void close(); // closes the window
void queue_window_state(int state); //sets up a window state to be activated
void add_state(WindowState* state); //adds a state to the window
void set_caption(std::string caption);
//destructor
~Window();
};
#endif
you have a circular include. Notice how WindowState.h includes Window.h and Window.h includes WindowState.h? It's a never ending dependency cycle that the compiler doesn't like.
If it were my money, I'd remove the #include <Window.h form WindowState.h and before the class somewhere do what is called a forward declaration: class Window;.
Keep in mind, you can' t use this class in this file. You can, however, include it and use it in the corresponding WindowState.cpp file.
In WindowsState.h both Window and Graphics do not need a complete definition, just a forward declaration, so change:
#include"Window.h"
#include"Graphics.h"
to:
class Window;
class Graphics;
In Window.h, WindowState only needs a forward declaration, so change:
#include "WindowsState.h"
to:
class WindowState;
Graphics cannot be forward-declared in Window.h because the full size of the type must be computed to compile Window successfully.
Using forward declarations minimizes compilation dependencies between files and makes your code compile faster. Changing the Graphics.h and Window.h files, for example, will not require recompiling all files that include only WindowState.h, for example.
I'm having a problem with circular dependencies, I suppose this is a design flaw from introducing the Game class in the wrong way.
Game.h:
#pragma once
#include <SFML\Graphics.hpp>
#include "GameScreen.h"
#include "TitleScreen.h"
class Game
{
protected:
sf::RenderWindow window;
GameScreen* CurrentGameScreen;
TitleScreen Title;
public:
Game(void);
~Game(void);
sf::RenderWindow getWindow(void);
void Run();
void Close();
};
GameScreen.h:
#pragma once
#include "Game.h"
class GameScreen
{
public:
GameScreen(void);
~GameScreen(void);
virtual void LoadAllResources() {};
virtual void DrawScreen(Game* game) {};
virtual void Update(Game* game) {};
};
TitleScreen.h:
#pragma once
#include <SFML\Graphics.hpp>
#include "GameScreen.h"
class TitleScreen : public virtual GameScreen
{
private:
sf::Texture title_screen;
sf::Sprite titleScreen;
sf::Font font;
sf::Text menuExit;
public:
TitleScreen(void);
~TitleScreen(void);
void LoadAllResources();
void DrawScreen(Game* game);
void Update(Game* game);
};
Then there's the main file:
#include "Game.h"
int main()
{
Game game;
game.Run();
//sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
//GameScreen* currentScreen;
//TitleScreen titleScreen;
//currentScreen = &titleScreen;
//while (window.isOpen())
//{
// currentScreen->Update(&window);
// currentScreen->DrawScreen(&window);
//}
return 0;
}
GameScreen.h and TitleScreen.h raise a handful of C2061. From what I understand these are caused by circular dependencies between Game.h and Gamescreen.h.
TitleScreen.h is giving me error C2504: 'GameScreen' : base class undefined.
Game.h: on lines 12 and 13, give C2143: syntax error : missing ';' before '*', although I'm not sure where this is coming from and my IDE is not giving me any syntax errors.
If I remove the #include statement from GameScreen.h and substitute it with a forward declaration class Game; (which I guess breaks the circular dependency?) most of the above is solved, but TitleScreen.cpp throws a set of C2027, C2227 and C2228 (undefined type, left of -> and left of .) every time I try to access a Game object. IntelliSense points out that a pointer to an incomplete class is not allowed.
I got it working before introducing the Game class - DrawScreen() and Update() would take as argument a pointer to window (sf::RenderWindow* window). There's part of the old code left in main.cpp.
In the GameScreen.h you should declare the Game class instead of including its whole header file, so this:
class Game;
instead of:
#include "Game.h"
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.