hi,
i am working on a 2D shooter. As i wanted to rework the code because its just spaghetti, i caused an error i cant fix myself and hope that someone of you guys can help me
i will post the code that i think will be relevant for solving the issue. if you need something more, please tell me.
//main.cpp
#include "Game.h"
int main()
{
Game pew;
pew.ChangeState(Game::gameStates::MAINMENU);
pew.Run();
return 0;
}
//Game.h
#ifndef GAME_H
#define GAME_H
#include <iostream>
#include <memory>
#include "GameState.h"
#include "MainMenuState.h"
#include "PlayState.h"
#include "Intro.h"
class Game
{
public:
Game();
~Game();
enum class gameStates{ MAINMENU, INTRO, SETTINGS, PLAY, GRAPHICSET, SOUNDSET, DIFFSET, COOPSET, HIGHSCORE };
void Run();
void ChangeState(gameStates newState);
bool isRunning(){ return running; };
void setRunning(bool mRunning);
sf::RenderWindow window;
private:
void Quit();
void Update();
void HandleEvents();
void Render();
std::unique_ptr<GameState> CurrentState;
bool running;
};
#endif
//Game.cpp
#include "Game.h"
Game::Game()
{
running = true;
window.create(sf::VideoMode(800, 600), "Pew");
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
}
Game::~Game()
{
CurrentState = nullptr;
}
void Game::Run()
{
while (running)
{
Update();
HandleEvents();
Render();
Quit();
}
}
void Game::ChangeState(gameStates newState)
{
switch (newState)
{
case gameStates::MAINMENU:
CurrentState = std::move(std::unique_ptr<MainMenuState>(new MainMenuState));
break;
case gameStates::PLAY:
CurrentState = std::move(std::unique_ptr<PlayState>(new PlayState));
break;
case gameStates::INTRO:
CurrentState = std::move(std::unique_ptr<Intro>(new Intro));
break;
}
}
void Game::setRunning(bool mRunning)
{
running = mRunning;
}
void Game::Quit()
{
if (!running)
window.close();
}
void Game::Update()
{
CurrentState->Update(*this);
}
void Game::HandleEvents()
{
CurrentState->HandleEvents(*this);
}
void Game::Render()
{
window.clear();
CurrentState->Render(*this);
window.display();
}
//MainMenuState.h
#ifndef MAINMENUSTATE_H
#define MAINMENUSTATE_H
#include "Game.h"
#include "MenuSfx.h"
#include "IOstuff.h"
class MainMenuState : public GameState
{
public:
MainMenuState();
~MainMenuState();
void HandleEvents(Game &game);
void Update(Game &game);
void Render(Game &game);
private:
IOsound iosound;
MenuSound sound;
bool playing;
float speed;
float elapsedTime;
float x_movement;
float y_movement;
float x, y;
short int debauch;
short int selection;
int volume;
sf::Event *pEvent;
sf::Clock *pClock;
sf::Texture *texture;
sf::Sprite *sprite;
Background bg;
Text play, again, settings, close;
};
#endif
//MainMenuState.cpp
#define PI 3.14159
#include "MainMenuState.h"
#include <cmath>
MainMenuState::MainMenuState()
{
bg.setFilePath("graphics//core//menu.png");
//sound & music
sound.LoadSoundBuffer();
sound.setBuffer(volume);
playing = false;
speed = 0.5;
selection = 0;
iosound.ReadSoundSettings(volume);
pEvent = new sf::Event;
pClock = new sf::Clock;
texture = new sf::Texture;
sprite = new sf::Sprite;
//Enemy
elapsedTime = 0;
x_movement = 0;
y_movement = 0;
debauch = 200;
texture->loadFromFile("graphics//enemies//enemy.png");
texture->setSmooth(false);
sprite->setTexture(*texture);
sprite->setOrigin(36.5, 35);
//buttons
play.setStringAndSize("play", 70);
again.setStringAndSize("again", 70);
settings.setStringAndSize("settings", 70);
close.setStringAndSize("close", 70);
play.setPosition(270, 150);
again.setPosition(270, 150);
settings.setPosition(270, 250);
close.setPosition(270, 350);
}
MainMenuState::~MainMenuState()
{
delete pEvent;
delete pClock;
delete sprite;
delete texture;
pEvent = nullptr;
pClock = nullptr;
sprite = nullptr;
texture = nullptr;
}
void MainMenuState::HandleEvents(Game &game)
{
while (game.window.pollEvent(*pEvent))
{
if (pEvent->type == sf::Event::Closed)
game.setRunning(false);
//Keyboard selection
if (pEvent->type == sf::Event::KeyPressed)
{
switch (pEvent->key.code)
{
case sf::Keyboard::Up:
if (selection > 0)
{
selection -= 1;
sound.PlaySound("select");
}
else
selection = 0;
break;
case sf::Keyboard::Down:
if (selection < 2)
{
selection += 1;
sound.PlaySound("select");
}
else
selection = 2;
break;
case sf::Keyboard::Return:
if (selection == 0)
{
if (playing)
game.ChangeState(Game::gameStates::PLAY);
else
playing = true;
game.ChangeState(Game::gameStates::INTRO);
}
else if (selection == 1)
game.ChangeState(Game::gameStates::SETTINGS);
else
game.setRunning(false);
break;
default:
break;
}
}
}
}
void MainMenuState::Update(Game &game)
{
if (selection == 0)//Spielen
{
play.setColor(sf::Color(255, 128, 0));
again.setColor(sf::Color(255, 128, 0));
settings.setColor(sf::Color(255, 255, 255));
close.setColor(sf::Color(255, 255, 255));
}
else if (selection == 1)//Settings
{
play.setColor(sf::Color(255, 255, 255));
again.setColor(sf::Color(255, 255, 255));
settings.setColor(sf::Color(255, 128, 0));
close.setColor(sf::Color(255, 255, 255));
}
else//Beenden
{
play.setColor(sf::Color(255, 255, 255));
again.setColor(sf::Color(255, 255, 255));
settings.setColor(sf::Color(255, 255, 255));
close.setColor(sf::Color(255, 128, 0));
}
//moving enemy
elapsedTime = pClock->restart().asMilliseconds();
x_movement += elapsedTime;
y_movement += elapsedTime;
y = sprite->getPosition().y;
x = sprite->getPosition().x;
y = 300 + std::sin((y_movement * PI) / 180) * debauch;
x = 400 + std::cos((x_movement * PI) / 180) * debauch;
if (x_movement > 360)
x_movement = 0;
if (y_movement > 360)
y_movement = 0;
sprite->setPosition(x, y);
}
void MainMenuState::Render(Game &game)
{
bg.Render(game.window);
game.window.draw(*sprite);
if (playing)
again.Render(game.window);
else
play.Render(game.window);
settings.Render(game.window);
close.Render(game.window);
}
as the picture is german, let me say that right to _Myptr and running it says <could not read memory>
the right table is the call stack.
the debug output says this:
Ausnahme (erste Chance) bei 0x50024EBE (sfml-window-2.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Schreiben an Position 0xFEEEFEEE
Ausnahmefehler bei 0x50024EBE (sfml-window-2.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Schreiben an Position 0xFEEEFEEE
Das Programm "[2032] Pew ReWork.exe" wurde mit Code 0 (0x0) beendet.
means something like: exception error at 0x50024EBE (sfml-window-2.dll) in Pew ReWork.exe: 0xC0000005: access violation when writing at position 0xFEEEFEEE
(freely translated, my english is a bit rusty)
thank you for your help