When I try to call OnLoop I get error that it doesn't recognise it.
///Ins_App.h
#ifndef INS_APP_H
#define INS_APP_H
#include <SDL/SDL.h>
class Ins_App
{
private:
/* Variables */
bool Running;
SDL_Surface* Surf_Display;
public:
/* inMain */
Ins_App();
int OnExecute();
public:
/* Other */
bool OnInit();
void OnEvent(SDL_Event* Event);
void OnLoop();
void OnRender();
void OnCleanup();
protected:
};
#endif // INS_APP_H
///Ins_App.cpp
#include "Ins_App.h"
Ins_App::Ins_App()
{
Running = true;
Surf_Display = NULL;
}
int Ins_App::OnExecute(){
if(OnInit() == false){
return -1;
}
SDL_Event Event;
while(Running){
while(SDL_PollEvent(&Event)){
OnEvent(&Event);
}
OnLoop();
OnRender();
}
return 0;
}
int main(int argc, char* argv[]){
Ins_App iApp;
return iApp.OnExecute();
}
///OnLoop.cpp
#include "Ins_App.h"
void OnLoop(){
}
And here is the error:
obj\Debug\src\Ins_App.o:C:\Users\Al\Documents\Ins
\src\Ins_App.cpp|19|undefined reference to `Ins_App::OnLoop()'|
What am I doing wrong?
You didn't define your member:
void OnLoop(){
}
should be
void Ins_App::OnLoop(){
}
You're basically just defining a free function named OnLoop, not your member.
Related
For my game, I want to use PhysFs to extract music files that are in a zip file
I created a custom class MusicStream that inherits from sf::InputStream that I use as an sf::Music's stream.
This is my basic program:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "musicstream.h"
#include "physfs.h"
int main() {
PHYSFS_init(0);
PHYSFS_addToSearchPath("data.zip", 0);
std::string musicFile = "music.ogg";
if (PHYSFS_exists(musicFile.c_str()) == 0) {
PHYSFS_deinit();
return EXIT_FAILURE;
}
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::Music myMusic;
MusicStream myStream(musicFile.c_str());
if (!myStream.getError()) {
myMusic.openFromStream(myStream);
myMusic.play();
}
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) window.close();
}
}
myMusic.stop();
PHYSFS_deinit();
return 0;
}
This works flawlessly, except for one thing:
When I close the window and the program exits, I'm getting a runtime error R6025 pure virtual function call and the program crashes.
So apparently a pure virtual function is called (sf::InputStream's dtor??), but I implemented all of sf::InputStream's functions and it doesn't make sense to me.
Also, I'm not really sure if the code is relevant but in case it is, this is the custom class:
musicstream.h
#ifndef MUSIC_STREAM_H_INCLUDED
#define MUSIC_STREAM_H_INCLUDED
#include <SFML/System.hpp>
#include "physfs.h"
class MusicStream : public sf::InputStream {
public:
MusicStream();
MusicStream(const char *fileName);
virtual ~MusicStream() override;
sf::Int64 read(void *data, sf::Int64) override;
sf::Int64 seek(sf::Int64 position) override;
sf::Int64 tell() override;
sf::Int64 getSize() override;
bool getError() const;
private:
PHYSFS_File *file_;
bool error_;
};
#endif
musicstream.cpp
#include "musicstream.h"
MusicStream::MusicStream() :
error_(true)
{
}
MusicStream::MusicStream(const char *filename) :
error_(false)
{
file_ = PHYSFS_openRead(filename);
if (file_ == nullptr) {
error_ = true;
}
}
MusicStream::~MusicStream() {
if (error_) { return; }
PHYSFS_close(file_);
}
sf::Int64 MusicStream::read(void *data, sf::Int64 size) {
if (error_) { return 0; }
sf::Int64 fileRead = PHYSFS_read(file_, data, 1, size);
if (fileRead == -1) {
return 0;
}
return fileRead;
}
sf::Int64 MusicStream::seek(sf::Int64 position) {
if (error_) { return -1; }
if (PHYSFS_seek(file_, position) == 0) {
return -1;
}
return position;
}
sf::Int64 MusicStream::tell() {
if (error_) { return -1; }
sf::Int64 position = PHYSFS_tell(file_);
return position;
}
sf::Int64 MusicStream::getSize() {
if (error_) { return -1; }
sf::Int64 size = PHYSFS_fileLength(file_);
return size;
}
bool MusicStream::getError() const {
return error_;
}
The problem were these two lines:
sf::Music myMusic;
MusicStream myStream(musicFile.c_str());
I swapped them and got rid of the error. It's because music is played in its own thread. It tried reading from the stream after it was destroyed. Now the music is destroyed before the stream is.
I'm trying to move the layout up when the keyboard is presented cause it hides my input textfield
but my app crash from line HASH_FIND_PTR(_targets, &tmp, element); from this class
void ActionManager::addAction(Action *action, Node *target, bool paused)
{
CCASSERT(action != nullptr, "");
CCASSERT(target != nullptr, "");
tHashElement *element = nullptr;
// we should convert it to Ref*, because we save it as Ref*
Ref *tmp = target;
HASH_FIND_PTR(_targets, &tmp, element); //error
if (! element)
{
element = (tHashElement*)calloc(sizeof(*element), 1);
element->paused = paused;
target->retain();
element->target = target;
HASH_ADD_PTR(_targets, target, element);
}
actionAllocWithHashElement(element);
CCASSERT(! ccArrayContainsObject(element->actions, action), "");
ccArrayAppendObject(element->actions, action);
action->startWithTarget(target);
}
I'm using cocos2d-x 3.2
i have debug and it's crashing after this line m_pLayout->runAction(moveBy);
anyone please give me some advice how can i solve it thanks
My LoginScene.h
#include "cocos2d.h"
#include "cocos-ext.h"
#include "CocosGUI.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace ui;
class LoginScene : public Scene
{
public:
LoginScene(bool pPortrait=false);
~LoginScene();
virtual void onEnter();
virtual void onExit();
void onLogin(Ref* pSender, Widget::TouchEventType type);
void onRegister(Ref* pSender, Widget::TouchEventType type);
void TextFieldEvent(Ref* pSender,TextField::EventType type);
protected:
Layout* m_pLayout;
Layer* m_pUILayer;
};
and LoginScene.cpp
#include "LoginScene.h"
#include "cocostudio/CCSSceneReader.h"
#include "cocostudio/CCSGUIReader.h"
#include "cocostudio/CCActionManagerEx.h"
#include <sqlite3.h>
LoginScene::LoginScene(bool pPortrait):m_pLayout(NULL),m_pUILayer(NULL)
{
Scene::init();
}
LoginScene::~LoginScene()
{
}
void LoginScene::onEnter()
{
Scene::onEnter();
m_pUILayer=Layer::create();
m_pUILayer->scheduleUpdate();
addChild(m_pUILayer);
//register root from json
m_pLayout=dynamic_cast<Layout*>(cocostudio::GUIReader::getInstance()->widgetFromJsonFile("LoginScene/LoginScene.json"));
m_pUILayer->addChild(m_pLayout);
//button initialize
Button *btnLogin=static_cast<Button*>(Helper::seekWidgetByName(m_pLayout, "btnLogin"));
btnLogin->addTouchEventListener(CC_CALLBACK_2(LoginScene::onLogin, this));
Button *btnRegister=static_cast<Button*>(Helper::seekWidgetByName(m_pLayout, "btnRegister"));
btnRegister->addTouchEventListener(CC_CALLBACK_2(LoginScene::onRegister, this));
//textfield initialize
TextField *txtUsername=static_cast<TextField*>(Helper::seekWidgetByName(m_pLayout, "txtUsername"));
txtUsername->addEventListenerTextField(m_pLayout, textfieldeventselector(LoginScene::TextFieldEvent));
TextField *txtPassword=static_cast<TextField*>(Helper::seekWidgetByName(m_pLayout, "txtPassword"));
txtPassword->addEventListenerTextField(m_pLayout, textfieldeventselector(LoginScene::TextFieldEvent));
}
void LoginScene::onExit()
{
m_pUILayer->removeFromParent();
cocostudio::GUIReader::destroyInstance();
cocostudio::ActionManagerEx::destroyInstance();
cocostudio::SceneReader::destroyInstance();
Scene::onExit();
}
void LoginScene::onLogin(Ref* pSender, Widget::TouchEventType type)
{
if(type==Widget::TouchEventType::ENDED)
{
CCLOG("abc");
}
}
void LoginScene::onRegister(Ref* pSender, Widget::TouchEventType type)
{
if (type==Widget::TouchEventType::ENDED)
{
CCLOG("abc");
}
}
void LoginScene::TextFieldEvent(Ref* pSender,TextField::EventType type)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
switch (type)
{
case TextField::EventType::ATTACH_WITH_IME:
{
TextField* txtUsername=dynamic_cast<TextField*>(pSender);
MoveBy* moveBy=MoveBy::create(0.5f, Vec2(0,txtUsername->getContentSize().height*2.5));
m_pLayout->runAction(moveBy);
}
break;
case TextField::EventType::DETACH_WITH_IME:
{
TextField* txtUsername=dynamic_cast<TextField*>(pSender);
MoveBy* moveBy=MoveBy::create(0.1f, Vec2(0,-txtUsername->getContentSize().height*2.5));
m_pLayout->runAction(moveBy);
}
break;
default:
break;
}
#endif
}
I'm new to programming and I'm trying to follow a book I bought... but even following the book's code I can't make the program to run.
======== (main.cpp) ========
#include "Game.h"
Game* g_game = 0;
int main(int argc, char* argv[])
{
g_game = new Game();
g_game -> init("Titulo", 100, 100, 800, 600, 0);
while(g_game->running())
{
g_game->handleEvents();
g_game->update();
g_game->render();
}
g_game->clean();
return 0;
}
======== (Game.cpp) ========
#include "Game.h"
using namespace std;
bool Game::init(const char* titulo, int xpos, int ypos, int altura, int largura, int flags)
{
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
m_pWindow = SDL_CreateWindow(titulo, xpos, ypos, altura, largura, flags);
if (m_pWindow != 0)
{
g_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if(m_pRenderer != 0)
{
SDL_SetRenderDrawColor(m_pRenderer, 255, 255,255, 255);
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
m_bRunning = true; //Começar o loop
return true;
}
======== (Game.h) ========
#ifndef _Game_
#define _Game_
#include <SDL.h>
class Game
{
public:
Game(){}
~Game(){}
void init() {m_bRunning = true; }
void render(){}
void update() {}
void handleEvents(){}
void clean(){}
bool running() { return m_bRunning; }
private:
SDL_Window* m_pWindow;
SDL_Window* m_pRenderer;
bool m_bRunning;
};
#endif /* defined(_Game_) */
But when I'm trying to compile I got the following error:
[Error] no matching function for call to 'Game::init(const char [7], int, int, int, int, int)'
[Note] candidate is: In file included from main.cpp
[Note] void Game::init()
[Note] candidate expects 0 arguments, 6 provided
Can someone help me and explain why did this happens? I read other topics about the missing of a default construct, but couldn't figure it out.
Your class declares (and defines)
void init() {m_bRunning = true; }
but there is no declaration for the overload you are later on trying to define. Add this:
bool init(const char* titulo, int xpos, int ypos, int altura, int largura, int flags);
to the class. Note the semicolon at the end of the declaration.
Generally speaking, you need to declare all members of a class in the class' definition. You can decide to define them directly (like your init() method) or later on (like the second init(...) method), but once a class has been defined, you can not simply add new members later on by defining them somewhere else.
I get this error
undefined reference to `InputHandler::InputHandler()' on line 22
I am working on a InputHandler Class in SDL
InputHandler.h
/*
* InputHandler.h
*
* Created on: 16 apr. 2014
* Author: JAN
*/
#ifndef INPUTHANDLER_H_
#define INPUTHANDLER_H_
#include "SDL2/SDL.h"
#include "Vector2D.h"
class InputHandler
{
public:
static InputHandler* Instance()
{
if(s_pInstance == 0)
{
s_pInstance = new InputHandler();
}
return s_pInstance;
}
void reset();
// update and clean the input handler
void update();
void clean();
// keyboard events
bool isKeyDown(SDL_Scancode key) const;
// joystick events
int getAxisX(int joy, int stick) const;
int getAxisY(int joy, int stick) const;
bool getButtonState(int joy, int buttonNumber) const;
// mouse events
bool getMouseButtonState(int buttonNumber) const;
Vector2D* getMousePosition() const;
private:
InputHandler();
~InputHandler();
InputHandler(const InputHandler&);
InputHandler& operator=(const InputHandler&);
// private functions to handle different event types
// handle keyboard events
void onKeyDown();
void onKeyUp();
// handle mouse events
void onMouseMove(SDL_Event& event);
void onMouseButtonDown(SDL_Event& event);
void onMouseButtonUp(SDL_Event& event);
// handle joysticks events
void onJoystickAxisMove(SDL_Event& event);
void onJoystickButtonDown(SDL_Event& event);
void onJoystickButtonUp(SDL_Event& event);
// member variables
// keyboard specific
const Uint8* m_keystates;
// singleton
static InputHandler* s_pInstance;
};
typedef InputHandler TheInputHandler;
#endif
InputHandler.cpp
/*
* InputHandler.cpp
*
* Created on: 16 apr. 2014
* Author: JAN
*/
#include "InputHandler.h"
#include "Game.h"
InputHandler* InputHandler::s_pInstance = 0;
bool InputHandler::isKeyDown(SDL_Scancode key) const
{
if(m_keystates != 0)
{
if(m_keystates[key] == 1)
{
return true;
}
else
{
return false;
}
}
return false;
}
void InputHandler::update()
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
onKeyDown();
break;
case SDL_KEYUP:
onKeyUp();
break;
default:
break;
}
}
}
void InputHandler::onKeyDown()
{
m_keystates = SDL_GetKeyboardState(0);
}
void InputHandler::onKeyUp()
{
m_keystates = SDL_GetKeyboardState(0);
}
I am new to programming in c++ and working with SDL. So it's probably a stupid error but if someone can explain me that would be great! Kinda stuck on this right now don't know what i am doing wrong
You haven't defined the default constructor anywhere.
Add it to the cpp file.
You seem to be missing quite a few other members, too.
In class InputHandler your constructor must be like this: InputHandler(){}
I seem to be having an issue with the use of SDL_Surface(s).
This is my header file (CApp.h):
#ifndef _CAPP_H_
#define _CAPP_H_
#include <SDL.h>
class CApp {
private:
bool Running;
SDL_Surface* Surf_Display;
public:
CApp();
int OnExecute();
public:
bool OnInit();
void OnEvent(SDL_Event* Event);
void OnLoop();
void OnRender();
void OnCleanup();
};
#endif
and this is where the problem lies:
#include "CApp.h"
CApp::CApp() {
Surf_Display = NULL;
Running = true;
}
int CApp::OnExecute() {
if(OnInit() == false) {
return -1;
}
SDL_Event Event;
while(Running) {
while(SDL_PollEvent(&Event)) {
OnEvent(&Event);
}
OnLoop();
OnRender();
}
OnCleanup();
return 0;
}
int main(int argc, char* argv[]) {
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Quit SDL
SDL_Quit();
return 0;
CApp theApp;
return theApp.OnExecute();
}
The error say 'Surf_Display' was not declared in this scope.