Hey I'm a C++ beginner and trying to learn the basics. I tried to compile my code but it keeps telling me
error. (c2653) "Framework": no class or namespace in line 4.
Another error is
"sf":no class or namespace
but if I add #include <SFML\Graphics.hpp> to my Framework.cpp code it solve the problem. As I understand the .hpp headers include all the code to the cpp files. So why should I include the SFML\Graphics.hpp twice?
Here is my Code:
#ifndef FRAMEWORKK_H
#define FRAMEWORKK_H
#include <iostream>
#include <SFML\Graphics.hpp>
#include "stdafx.h"
class Framework
{
public:
Framework();
~Framework();
void run();
private:
void quit();
void update();
void handleEvent();
void render();
sf::RenderWindow * pRenderWindow;
sf::Event * pMainEvent;
bool mRun;
};#endif
and here is my .cpp code
#include "Frameworkk.h"
#include "stdafx.h"
Framework::Framework(){
pRenderWindow = new sf::RenderWindow(sf::VideoMode(800, 600, 32),
"Spiel");
pMainEvent = new sf::Event;
mRun = true;
}
Framework::~Framework()
{
}
void Framework::run()
{
while (mRun)
{
update();
handleEvent();
render();
quit();
}
}
void Framework::quit()
{
if (!mRun)
{
pRenderWindow->close();
}
}
void Framework::update()
{
}
void Framework::handleEvent()
{
while (pRenderWindow->pollEvent(*pMainEvent))
{
if (pMainEvent->type == sf::Event::Closed)
{
mRun = false;
}
}
}
void Framework::render()
{
pRenderWindow->clear(sf::Color(120, 120, 120));
pRenderWindow->display();
}
Related
I want to display my player in the window but my player sprite is not showing in the window. I am new to c++. I want to learn classes, inheritence, composition, etc in this way.I have 3 files Player.cpp, Game.cpp and main.cpp. I am using main.cpp to call Game.cpp using a fuction called Run().
Got nothing to try.
Player.cpp
#include "Player.hpp"
#include "string.h"
#include <iostream>
void Player::initPlayer()
{
const char* playerTexturePath = "/Users/don/Desktop/sfmlgames/game1/img/MCmid.png";
if(!mPlayerTexture.loadFromFile(playerTexturePath))
{
std::cout << "mPlayerTexturePath not found!!" << std::endl;
}
mPlayerSprite.setTexture(mPlayerTexture);
mPlayerSprite.setPosition(100.f, 100.f);
mPlayerSprite.setScale(1.f, 1.f);
}
void Player::draw_player(sf::RenderWindow &win)
{
win.draw(mPlayerSprite);
}
Game.cpp
#include "Game.hpp"
#include <iostream>
Player player1;
//Constructor: Create a window and Player
Game::Game() : mWindow(sf::VideoMode(640, 480), "Game")
{
//Frame rate 60
mWindow.setFramerateLimit(60);
player1.initPlayer();
}
//Game loop
void Game::Run()
{
while(mWindow.isOpen())
{
render();
events();
update();
}
}
void Game::events()
{
sf::Event event;
while (mWindow.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
mWindow.close();
}
}
}
void Game::update()
{
}
void Game::render()
{
mWindow.clear(sf::Color::Red);
player1.draw_player(mWindow);
mWindow.display();
}
main.cpp
#include "Game.hpp"
int main()
{
Game game;
game.Run();
}
I don't think I will need to give code to hpp files.
The problem was with image I dont know why. I used another image and it worked fine.
New to C++, I'm trying to make "platforms" spawn every 2 seconds and go upwards. However, the platform disappears when a new Platform is pushed onto the platforms vector.
Platform.h:
#pragma once
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <vector>
class Platform
{
private:
sf::RectangleShape shape;
public:
Platform() = default;
Platform(const Platform& platform);
void updatePos();
sf::RectangleShape getShape() { return shape; }
};
Platform.cpp:
#include "Platform.h"
Platform::Platform(const Platform& platform)
{
shape.setSize(sf::Vector2f(300.f, 40.f));
shape.setPosition(1920.f / 2.f - shape.getSize().x / 2.f, 1080.f);
}
void Platform::updatePos()
{
shape.move(sf::Vector2f(0.f, -5.f));
}
Game.h:
#pragma once
#include "Platform.h"
#include <iostream>
class Game
{
private:
sf::RenderWindow window{ sf::VideoMode(1920, 1080), "Dodge the Spikes", sf::Style::Fullscreen };
std::vector<Platform> platforms;
bool gameOver{};
unsigned int platformTimer{};
public:
Game() = default;
void update();
void draw();
void run();
};
Game.cpp:
#include "Game.h"
void Game::update()
{
if (!gameOver)
{
//spawn platforms
platformTimer++;
if (platformTimer >= 120)
{
platforms.push_back(Platform{});
platformTimer = 0;
}
//update platforms
for (auto i{platforms.begin()}; i < platforms.end();)
{
i->updatePos();
i++;
}
}
}
void Game::draw()
{
for (auto i{ platforms.begin() }; i < platforms.end();)
{
window.draw(i->getShape());
i++;
}
}
void Game::run()
{
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
if (evnt.type == sf::Event::Closed)
{
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
window.close();
}
}
this->update();
window.clear();
this->draw();
window.display();
}
}
im currently making on my 2D game and i have a error that i dont understand, here is screenshot of the rror:
enter image description here
here is code for main:
#include "Game.h"
#include "Player.h"
void Run(RenderWindow &window, Sprite &player, Texture &PlayerTex);
int main(RenderWindow &window, Sprite &player, Texture &PlayerTex) {
Run(window, player, PlayerTex);
return 0;
}
void InitWindow() {
RenderWindow window(VideoMode(1920, 1080), "Discord Game");
}
void Update() {
}
void Render(RenderWindow &window, Sprite &player) {
player.setScale(0.3f, 0.3f);
window.clear(Color::White);
window.draw(player);
window.display();
}
void Run(RenderWindow &window, Sprite &player, Texture &PlayerTex) {
InitWindow();
while (window.isOpen()) {
Event sfEvent;
while (window.pollEvent(sfEvent)) {
if (sfEvent.type == Event::Closed)
window.close();
}
Update();
Render(window, player);
Player P;
P.PlayerMovement(player);
}
}
code for Player.H:
#pragma once
#include "Game.h"
class Player {
public:
Sprite player;
Texture PlayerTex;
void PlayerTexture(Sprite &Player, Texture &PlayerTex);
void PlayerMovement(Sprite &Player);
};
code for Player.cpp:
#include "Game.h"
#include "Player.h"
void Player::PlayerTexture(Sprite& player, Texture& PlayerTex) {
PlayerTex.loadFromFile("textures/Player/PlayerFront.png");
player.setTexture(PlayerTex);
}
void Player::PlayerMovement(Sprite &player) {
if (Keyboard::isKeyPressed(Keyboard::A)) {
player.move(-5.f, 0.f);
}
if (Keyboard::isKeyPressed(Keyboard::D)) {
player.move(5.f, 0.f);
}
if (Keyboard::isKeyPressed(Keyboard::W)) {
player.move(0.f, -5.f);
}
if (Keyboard::isKeyPressed(Keyboard::S)) {
player.move(0.f, 5.f);
}
}
and then i have Game.H just to include stuff and for the future, here is code for Game.h:
#pragma once
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
using namespace sf;
class Game {
};
and then empty Game.cpp:
#include "Game.h"
Been looking for awhile and I cant find an answer. All paths are included. I'm referencing a class in another namespace in the class I'm creating.
I'm getting the following error:
src/app/Application.h:30:9: error: 'drv' does not name a type
Code Below. Any help is appreciated!
Main.cpp
int main(int argc, char** argv) {
app::Application program;
program.run();
return 0;
}
LEDs.h
#ifndef LEDS_H
#define LEDS_H
namespace drv {
class LEDs {
public:
LEDs();
void InitLEDs(void);
void SetLEDs(const uint8_t value);
private:
static const uint8_t NUM_LEDs = 5;
};
}
#endif /* LEDS_H */
Application.h
#ifndef APPLICATION_H
#define APPLICATION_H
namespace app {
enum State {
NORMAL = 0,
ZONE,
PAIRING,
STUCK,
BATT,
OFF
};
class Application {
public:
Application();
void run(void);
void execute_loop(void);
private:
bool IDLE;
State STATE;
drv::LEDs Leds; // LINE 30
};
}
#endif // APPLICATION_H
Application.cpp
#include "stdint.h"
#include "stdbool.h"
#include "../drv/LEDs.h"
#include "Application.h"
namespace app {
Application::Application() {
IDLE = false;
STATE = NORMAL;
}
void Application::run(void) {
Leds.InitLEDs();
while(1)
{
if(IDLE) {
PowerSaveIdle();
}
else {
execute_loop();
}
}
}
void Application::execute_loop(void)
{
}
}
LEDs.cpp
#include <stdint.h>
#include "LEDs.h"
namespace drv {
LEDs::LEDs() {
}
void LEDs::InitLEDs() {
SetLEDs(0xff);
}
void LEDs::SetLEDs(const uint8_t value) {
//Removed for readability
}
}
You're missing an #include ... line in Application.h. At the top of that file (or just after the inclusion guards) add the line
#include "LEDs.h"
I am trying to create a game with c++ and SMFL 2.0 and i cant get pass this error and it's driving me crazy. I have been reading around the internet and it seems like it can be caused because of forgotten includes, but I still cant see where I have gone wrong. I have included the SMFL 2.0 library in the project properties section under c++ -> general -> additional include directories and under linker -> general -> additional library directories.
I'm completely blank and this is the error message i get:
1>------ Build started: Project: Pang, Configuration: Debug Win32 ------
1> Game.cpp
1> Generating Code...
1> Skipping... (no relevant changes detected)
1> Pang.cpp
1>Game.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
1>Game.obj : error LNK2001: unresolved external symbol "private: static class PlayerPaddle Game::_player1" (?_player1#Game##0VPlayerPaddle##A)
1>C:\Users\Alexander\Desktop\C++\Projects\Pang\Debug\Pang.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Microsoft Visual C++ doesnt show any syntax grammar errors with red underline when im looking around the code so it looks like the syntax is ok. Here are the relevant lines of code:
I have most of my general includes here:
stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <map>
#include <iostream>
#include <cassert>
#include <list>
VisibleGameObject.h
#pragma once
class VisibleGameObject {
public:
VisibleGameObject();
virtual ~VisibleGameObject();
virtual void Load(std::string filename);
virtual void Draw(sf::RenderWindow &window);
virtual void SetPosition(float x, float y);
private:
sf::Sprite _sprite;
sf::Texture _image;
std::string _filename;
bool _isLoaded;
};
VisibleGameObject.cpp
#include "stdafx.h"
#include "VisibleGameObject.h"
VisibleGameObject::VisibleGameObject() {
_isLoaded = false;
}
VisibleGameObject::~VisibleGameObject() {
}
void VisibleGameObject::Load(std::string filename) {
if (_image.loadFromFile(filename) == false) {
_filename = "";
_isLoaded = false;
}
else {
_filename = filename;
_sprite.setTexture(_image);
}
}
void VisibleGameObject::Draw(sf::RenderWindow &renderWindow) {
if (_isLoaded) {
renderWindow.draw(_sprite);
}
}
void VisibleGameObject::SetPosition(float x, float y) {
if (_isLoaded) {
_sprite.setPosition(x,y);
}
}
This class helps me use the SMFL library to draw images on the rendered window.
PlayerPaddle.h
#pragma once
#include "VisibleGameObject.h"
class PlayerPaddle : public VisibleGameObject {
public:
PlayerPaddle();
~PlayerPaddle();
};
PlayerPaddle.cpp
#include "stdafx.h"
#include "VisibleGameObject.h"
#include "PlayerPaddle.h"
PlayerPaddle::PlayerPaddle() {
}
PlayerPaddle::~PlayerPaddle() {
}
Game.h
#pragma once
#include "PlayerPaddle.h"
class Game {
public:
static void Start();
private:
static bool IsExiting();
static void GameLoop();
static void ShowSplashScreen();
static void ShowMenu();
enum GameState { Uninitialized, ShowingSplash, Paused, ShowingMenu, Playing, Exiting };
static GameState _gameState;
static sf::RenderWindow _mainWindow;
static PlayerPaddle _player1;
};
Game.cpp
#include "stdafx.h"
#include "Game.h"
#include "SplashScreen.h"
#include "MainMenu.h"
void Game::Start(void) {
if(_gameState != Uninitialized) {
return;
}
_mainWindow.create(sf::VideoMode(1024,768,32),"Pang!");
//_player1.Load("images/paddle.png");
//_player1.SetPosition((1024/2)-45, 700);
_gameState = Game::ShowingSplash;
while(!IsExiting()) {
GameLoop();
}
_mainWindow.close();
}
bool Game::IsExiting() {
if(_gameState == Game::Exiting) {
return true;
}
else {
return false;
}
}
void Game::GameLoop() {
switch(_gameState) {
case Game::ShowingMenu: {
ShowMenu();
break;
}
case Game::ShowingSplash: {
ShowSplashScreen();
break;
}
case Game::Playing: {
sf::Event currentEvent;
while (_mainWindow.pollEvent(currentEvent)) {
_mainWindow.clear(sf::Color(0,0,0));
//_player1.Draw(_mainWindow);
_mainWindow.display();
if (currentEvent.type == sf::Event::Closed) {
_gameState = Game::Exiting;
}
if (currentEvent.type == sf::Event::KeyPressed) {
return;
if(currentEvent.key.code == sf::Keyboard::Escape) {
ShowMenu();
}
}
}
break;
}
}
}
void Game::ShowSplashScreen() {
SplashScreen splashScreen;
splashScreen.Show(_mainWindow);
_gameState = Game::ShowingMenu;
}
void Game::ShowMenu() {
MainMenu mainMenu;
MainMenu::MenuResult result = mainMenu.Show(_mainWindow);
switch(result) {
case MainMenu::Exit: {
_gameState = Game::Exiting;
break;
}
case MainMenu::Play: {
_gameState = Game::Playing;
break;
}
}
}
Game::GameState Game::_gameState = Uninitialized;
sf::RenderWindow Game::_mainWindow;
This code compiles and runs ONLY when those 3 lines of codes starting with _player1 in Game.cpp are commented out. When not commented out i get this LNK2001 error and I just cant get passed this and it is quite frustrating.
Would be nice if someone had input on what could be wrong here.
Best of regards,
Alexander
Your game.cpp misses a definition of Game::_player1:
PlayerPaddle Game::_player1;
You properly define the static member variables _gameState and _mainWindow, but not _player1.
1) Try inheriting publicily PlayerPaddle class to Game class.It works.
Example
class Game:public PlayerPaddle
2) or you can use composition.Use a pointer or static pointer to PlayerPaddle in Game.
Use
static PlayerPaddle* _player1;
It should work.