Project Crashes after Drawing the menu screen

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Zyinto
Posts: 2
Joined: Mon Dec 09, 2013 11:20 am

Project Crashes after Drawing the menu screen

Post by Zyinto »

I'm trying to draw a menu screen before loading the level of my game, The menu gets drawn and right after that the project crashes.

This is my main

Code: Select all

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "Irrlicht.lib")
#endif
 
#include <irrlicht.h>
#include <iostream>
#include <driverChoice.h>
#include <IImage.h>
#include <SColor.h>
#include <IVideoDriver.h>
#include "MyEventReceiver.h"
#include "c_Player.h"
#include "Gamescreens.h"
 
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
core::vector3df CameraLookAt;
core::vector3df CameraPos;
 
MyEventReceiver receiver;
c_Player player;
 
int lastFPS = -1;
const f32 MOVEMENT_SPEED = 15.f;
int inmenu =1;
 
int main(int argc, char** argv)
{
 
 
 
    IrrlichtDevice *device =
        createDevice(EDT_OPENGL, dimension2d<u32>(1280, 720), 16,false, false, false, &receiver);
 
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();
 
 
    guienv->addStaticText(L"Outer Space 2d! ",
        rect<int>(10,10,200,22), true);
 
 
   //driver->setFog(video::SColor(255,255,255,255),EFT_FOG_LINEAR , 5, 1, 0.01f, false,false);
 
    scene::IAnimatedMeshSceneNode* Tile[64][64];
    video::IImage* TileMapImage = driver->createImageFromFile("res/tiletest.png");
 
 
 
 
    for(int h = 0 ; h < 64; h ++ )
    {
        for(int w = 0; w < 64 ; w ++ )
        {
            video::SColor PixelColor = TileMapImage->getPixel(h,w);
 
 
                if (PixelColor == video::SColor(255,255,0,0)  )
                {
                    printf("found red\n");
 
                Tile[h][w] = smgr->addAnimatedMeshSceneNode(smgr->getMesh("res/walltile_1_180.X"));
                printf("tile loaded\n");
                Tile[h][w]->setPosition(core::vector3df(h*3,0,w*3));
                Tile[h][w]->setMaterialFlag(EMF_LIGHTING,false);
                }
 
                if (PixelColor == video::SColor(255,255,255,255)  )
                {
                    printf("found white\n");
 
                Tile[h][w] = smgr->addAnimatedMeshSceneNode(smgr->getMesh("res/floortile_1.X"));
                printf("tile loaded\n");
                Tile[h][w]->setPosition(core::vector3df(h*3,0,w*3-3));
                Tile[h][w]->setMaterialFlag(EMF_LIGHTING,false);
                }
 
                if (PixelColor == video::SColor(255,255,150,0)  )
                {
                    printf("found orange\n");
 
                Tile[h][w] = smgr->addAnimatedMeshSceneNode(smgr->getMesh("res/dungeondoor_1_180.X"));
                printf("tile loaded\n");
                Tile[h][w]->setPosition(core::vector3df(h*3,0,w*3));
                Tile[h][w]->setMaterialFlag(EMF_LIGHTING,false);
                }
        }
    }
 
 
 
    CameraLookAt = vector3df(0,2,0);
    CameraPos = vector3df(0,2,10);
 
 
 
    while(device->run())
    {
        do{
        TitelScreen(driver,smgr);
        if(receiver.IsKeyDown(irr::KEY_RETURN))
            {
            inmenu = 0;
            printf("Enter\n");
            }
        }while(inmenu==1);
 
        //core::vector3df nodePosition = node->getPosition();
 
        ICameraSceneNode * MainCam = smgr->addCameraSceneNode(0,CameraPos, CameraLookAt);
        MainCam ->setFarValue(40.f);
 
        player.Handle();
 
 
        // drawing of the scene
        //driver->beginScene(true, true, video::SColor(255,113,113,133));
 
 
 
        //smgr->drawAll(); // draw the 3d scene
        // hier staat de nieuwe image draw
 
 
 
        //driver->endScene();
 
        int fps = driver->getFPS();
 
        if (lastFPS != fps)
        {
            core::stringw tmp(L"Movement Example - Irrlicht Engine [");
            tmp += driver->getName();
            tmp += L"] fps: ";
            tmp += fps;
 
            device->setWindowCaption(tmp.c_str());
            lastFPS = fps;
        }
    }
 
    device->drop();
 
 
    return 0;
}
 
and this is my gamescreens with the function titlescreen which draws the menu.

Code: Select all

void TitelScreen(IVideoDriver* menudriver,ISceneManager* menusmgr){
    printf("Crash\n");
    video::ITexture* menu = menudriver->getTexture("Screens/menu.png");
    printf("Crash2\n");
 
 
    menudriver->beginScene(true, true, video::SColor(255,113,113,133));
    menudriver->makeColorKeyTexture(menu, core::position2d<s32>(0,0));
    menusmgr->drawAll();
    menudriver->draw2DImage(menu, core::position2d<s32>(0,0));
    menudriver->endScene();
 
}
 
and as I mentioned earlier the menu does get drawn but for some reason it crashes almost instantly after it has been drawn.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Project Crashes after Drawing the menu screen

Post by Seven »

You are creating a new camera every frame
jiangcaiyang
Posts: 9
Joined: Tue Nov 12, 2013 3:24 pm

Re: Project Crashes after Drawing the menu screen

Post by jiangcaiyang »

Check for pointers, it often invokes methods with empty pointer. Be careful.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Project Crashes after Drawing the menu screen

Post by Seven »

Because title screen is called in a loop and you create a new image inside title screen you are creating a new image every frame. Create the image only once and then use the single instance over and over in the loop. Same thing with the camera
Zyinto
Posts: 2
Joined: Mon Dec 09, 2013 11:20 am

Re: Project Crashes after Drawing the menu screen

Post by Zyinto »

Seven wrote:Because title screen is called in a loop and you create a new image inside title screen you are creating a new image every frame. Create the image only once and then use the single instance over and over in the loop. Same thing with the camera
Thanks this was indeed my problem fixed it now!
Post Reply