Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Why?  (Read 10482 times)

0 Members and 1 Guest are viewing this topic.

buggy123

  • Newbie
  • *
  • Posts: 26
    • View Profile
Why?
« on: August 30, 2009, 11:55:32 pm »
So, I'm trying to draw a menu screen. Here's the header file and the main.cpp:

Code: [Select]
#include <SFML/Graphics.hpp>


//declarations
#define MENU 1
#define GAME 2
#define END 3

int game_state = MENU;

sf::RenderWindow App(sf::VideoMode::GetMode(1), "SFML Window");

// GRAPHICS

int load_graphics_menu(void)
{

//menu background

sf::Image menu_background;
if (!menu_background.LoadFromFile("data/art/menu/menu_background.bmp"))
{
MessageBox(NULL,
  "Fail to load image from file",
  "Error",
  NULL);

return -1;
} //Load image

sf::Sprite spt_menu_background;
spt_menu_background.SetImage(menu_background);

//menu play button

sf::Image menu_playbutton;
if (!menu_playbutton.LoadFromFile("data/art/menu/menu_playbutton.bmp"))
{
MessageBox(NULL,
  "Fail to load image from file",
  "Error",
  NULL);

return -1;
} //Load image

sf::Sprite spt_menu_playbutton;
spt_menu_playbutton.SetImage(menu_playbutton);
spt_menu_playbutton.SetPosition(475.f, 235.f);

//menu play button highlighted

sf::Image menu_playbutton_hl;
if (!menu_playbutton_hl.LoadFromFile("data/art/menu/menu_playbutton_hl.bmp"))
{
MessageBox(NULL,
  "Fail to load image from file",
  "Error",
  NULL);

return -1;
} //Load image

sf::Sprite spt_menu_playbutton_hl;
spt_menu_playbutton_hl.SetImage(menu_playbutton_hl);
spt_menu_playbutton_hl.SetPosition(475.f, 235.f);

//menu instruction button

sf::Image menu_instructionbutton;
if (!menu_instructionbutton.LoadFromFile("data/art/menu/menu_instructionbutton.bmp"))
{
MessageBox(NULL,
  "Fail to load image from file",
  "Error",
  NULL);

return -1;
} //Load image

sf::Sprite spt_menu_instructionbutton;
spt_menu_instructionbutton.SetImage(menu_instructionbutton);
spt_menu_instructionbutton.SetPosition(426.f, 324.f);

//menu instruction button highlighted

sf::Image menu_instructionbutton_hl;
if (!menu_instructionbutton_hl.LoadFromFile("data/art/menu/menu_instructionbutton_hl.bmp"))
{
MessageBox(NULL,
  "Fail to load image from file",
  "Error",
  NULL);

return -1;
} //Load image

sf::Sprite spt_menu_instructionbutton_hl;
spt_menu_instructionbutton_hl.SetImage(menu_instructionbutton_hl);
spt_menu_instructionbutton_hl.SetPosition(426.f, 324.f);
//menu exit button

sf::Image menu_exitbutton;
if (!menu_exitbutton.LoadFromFile("data/art/menu/menu_exitbutton.bmp"))
{
MessageBox(NULL,
  "Fail to load image from file",
  "Error",
  NULL);

return -1;
} //Load image

sf::Sprite spt_menu_exitbutton;
spt_menu_exitbutton.SetImage(menu_exitbutton);
spt_menu_exitbutton.SetPosition(12.f, 580.f);

//menu exit button highlighted

sf::Image menu_exitbutton_hl;
if (!menu_exitbutton_hl.LoadFromFile("data/art/menu/menu_exitbutton_hl.bmp"))
{
MessageBox(NULL,
  "Fail to load image from file",
  "Error",
  NULL);

return -1;
} //Load image

sf::Sprite spt_menu_exitbutton_hl;
spt_menu_exitbutton_hl.SetImage(menu_exitbutton_hl);
spt_menu_exitbutton_hl.SetPosition(12.f, 580.f);

return 0;
}

//INPUT

int process_input_menu(void)
 
{
sf::Event Event;
    while (App.GetEvent(Event))
    {
        // Window closed
        if (Event.Type == sf::Event::Closed)
            App.Close();

        // Escape key pressed
        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
            App.Close();
    }
}


main.cpp

Code: [Select]
#include "UltrapingMenu.h"

int main()
{
sf::RenderWindow App(sf::VideoMode::GetMode(1), "SFML Window");

App.Create(sf::VideoMode(1024, 768, 32), "Ultraping", sf::Style::Fullscreen); //Windows creation...somehow, (1) works, (0) doesn't

load_graphics_menu();

while (App.IsOpened() && game_state == MENU)
{
process_input_menu();

App.Clear();

App.Draw(spt_menu_background);
App.Draw(spt_menu_playbutton);
App.Draw(spt_menu_instructionbutton);
App.Draw(spt_menu_exitbutton);

App.Display();
}

    return 0;
}


and here's the errors

Code: [Select]
Compiling...
main.cpp
.\main.cpp(17) : error C2065: 'spt_menu_background' : undeclared identifier
.\main.cpp(18) : error C2065: 'spt_menu_playbutton' : undeclared identifier
.\main.cpp(19) : error C2065: 'spt_menu_instructionbutton' : undeclared identifier
.\main.cpp(20) : error C2065: 'spt_menu_exitbutton' : undeclared identifier


it doesn't make any sense since I obviously declared these in my load_graphics_menu section...why do I keep getting this error?

Also, does anyone have any tips on optimizing my code? It's a bit cumbersome right now and I'm not sure how to do some things. for example, if I wanted to draw a seperate image the the mouse is hovering over the play button, how should I do that? I don't want to use pages of if and thens.

phear-

  • Jr. Member
  • **
  • Posts: 64
    • MSN Messenger - LOApokalypse@hotmail.com
    • View Profile
    • http://gtproductions.org
Why?
« Reply #1 on: August 31, 2009, 02:36:15 am »
Look at the scope of those variables... they arent global. they are only within the load_graphics_menu() function
Eugene Alfonso
GTP | Twitter

phear-

  • Jr. Member
  • **
  • Posts: 64
    • MSN Messenger - LOApokalypse@hotmail.com
    • View Profile
    • http://gtproductions.org
Why?
« Reply #2 on: August 31, 2009, 02:56:07 am »
I redid some of your code, this should work:

Code: [Select]

#include <SFML/Graphics.hpp>
#include <windows.h>

enum GameState
{
MENU = 1,
GAME,
END
};

GameState CurrentState = MENU;

//Function declarations
int process_input_menu();
int load_graphics_menu();
int ErrorBox();

sf::RenderWindow App;

//Global sprites: All functions can use them
sf::Sprite spt_menu_background;
sf::Sprite spt_menu_playbutton;
sf::Sprite spt_menu_playbutton_hl;
sf::Sprite spt_menu_instructionbutton;
sf::Sprite spt_menu_instructionbutton_hl;
sf::Sprite spt_menu_exitbutton;
sf::Sprite spt_menu_exitbutton_hl;

int main()
{
App.Create(sf::VideoMode(1024, 768, 32), "Ultraping", sf::Style::Fullscreen);

if (load_graphics_menu() == EXIT_FAILURE)
return EXIT_FAILURE;

while (App.IsOpened() && CurrentState == MENU)
{
process_input_menu();

App.Clear();

App.Draw(spt_menu_background);
App.Draw(spt_menu_playbutton);
App.Draw(spt_menu_instructionbutton);
App.Draw(spt_menu_exitbutton);
           
App.Display();
}

return EXIT_SUCCESS;
}

//INPUT
int process_input_menu()
{
sf::Event Event;
while (App.GetEvent(Event))
{
// Window closed
if (Event.Type == sf::Event::Closed)
App.Close();

// Escape key pressed
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}
}

// GRAPHICS
int load_graphics_menu()
{
//////////////////////////////////////////////////////////
//menu background
sf::Image menu_background;
if (!menu_background.LoadFromFile("data/art/menu/menu_background.bmp"))
return ErrorBox();
spt_menu_background.SetImage(menu_background);

//////////////////////////////////////////////////////////
//menu play button
sf::Image menu_playbutton;
if (!menu_playbutton.LoadFromFile("data/art/menu/menu_playbutton.bmp"))
return ErrorBox();
spt_menu_playbutton.SetImage(menu_playbutton);
spt_menu_playbutton.SetPosition(475.f, 235.f);

//////////////////////////////////////////////////////////
//menu play button highlighted
sf::Image menu_playbutton_hl;
if (!menu_playbutton_hl.LoadFromFile("data/art/menu/menu_playbutton_hl.bmp"))
return ErrorBox();
spt_menu_playbutton_hl.SetImage(menu_playbutton_hl);
spt_menu_playbutton_hl.SetPosition(475.f, 235.f);

//////////////////////////////////////////////////////////
//menu instruction button
sf::Image menu_instructionbutton;
if (!menu_instructionbutton.LoadFromFile("data/art/menu/menu_instructionbutton.bmp"))
return ErrorBox();
spt_menu_instructionbutton.SetImage(menu_instructionbutton);
spt_menu_instructionbutton.SetPosition(426.f, 324.f);

//////////////////////////////////////////////////////////
//menu instruction button highlighted
sf::Image menu_instructionbutton_hl;
if (!menu_instructionbutton_hl.LoadFromFile("data/art/menu/menu_instructionbutton_hl.bmp"))
return ErrorBox();
spt_menu_instructionbutton_hl.SetImage(menu_instructionbutton_hl);
spt_menu_instructionbutton_hl.SetPosition(426.f, 324.f);

//////////////////////////////////////////////////////////
//menu exit button
sf::Image menu_exitbutton;
if (!menu_exitbutton.LoadFromFile("data/art/menu/menu_exitbutton.bmp"))
return ErrorBox();
spt_menu_exitbutton.SetImage(menu_exitbutton);
spt_menu_exitbutton.SetPosition(12.f, 580.f);

//////////////////////////////////////////////////////////
//menu exit button highlighted
sf::Image menu_exitbutton_hl;
if (!menu_exitbutton_hl.LoadFromFile("data/art/menu/menu_exitbutton_hl.bmp"))
return ErrorBox();
spt_menu_exitbutton_hl.SetImage(menu_exitbutton_hl);
spt_menu_exitbutton_hl.SetPosition(12.f, 580.f);

return EXIT_SUCCESS;
}

int ErrorBox()
{
MessageBox(NULL, L"Fail to load image from file", L"Error", NULL);
return EXIT_FAILURE;
}
Eugene Alfonso
GTP | Twitter

buggy123

  • Newbie
  • *
  • Posts: 26
    • View Profile
Why?
« Reply #3 on: August 31, 2009, 04:13:45 am »
k thanks...guess I didn't check carefully enough. btw, it's possible to load images through folders right? I have the folder in the same directory as the application and I'm sure the path is correct but it keeps failing to load O_o

phear-

  • Jr. Member
  • **
  • Posts: 64
    • MSN Messenger - LOApokalypse@hotmail.com
    • View Profile
    • http://gtproductions.org
Why?
« Reply #4 on: August 31, 2009, 04:17:55 am »
I'm not sure actually, maybe you should try putting a / before the first folder.
/blah/blah/blah/blah.bmp
Eugene Alfonso
GTP | Twitter

RixCoder

  • Newbie
  • *
  • Posts: 40
    • View Profile
Why?
« Reply #5 on: August 31, 2009, 04:22:17 am »
Yes you can load through folders. Remember, when loading through folders like that it will check to see if that folder exists where the project file is located. So if that data folder is not in the same directory as your vcproj ( or w/e IDE project file you have is )  you will have to compensate for that.

Use stuff like putting ".." in front of data/art/menu/menu_playbutton_hl.bmp to look like "../data/art/menu/menu_playbutton_hl.bmp" to navigate back one folder and check if the data one exists etc. You can chain em too.

-Stefan

buggy123

  • Newbie
  • *
  • Posts: 26
    • View Profile
Why?
« Reply #6 on: August 31, 2009, 04:50:57 am »
hmm...doesn't seem to be working. :(  I tried both ways and no avail. thanks for trying though guys. appreciated it

phear-

  • Jr. Member
  • **
  • Posts: 64
    • MSN Messenger - LOApokalypse@hotmail.com
    • View Profile
    • http://gtproductions.org
Why?
« Reply #7 on: August 31, 2009, 05:10:22 am »
What compiler are you using?
Eugene Alfonso
GTP | Twitter

buggy123

  • Newbie
  • *
  • Posts: 26
    • View Profile
Why?
« Reply #8 on: August 31, 2009, 05:33:34 am »
I'm using visual c++ 2008 express edition

phear-

  • Jr. Member
  • **
  • Posts: 64
    • MSN Messenger - LOApokalypse@hotmail.com
    • View Profile
    • http://gtproductions.org
Why?
« Reply #9 on: August 31, 2009, 05:49:56 am »
And you have the folders where your exe is? What error are you getting?
Eugene Alfonso
GTP | Twitter

buggy123

  • Newbie
  • *
  • Posts: 26
    • View Profile
Why?
« Reply #10 on: August 31, 2009, 06:14:22 am »
I'm getting the message box error I set up. And yes, I have the data folder right next to my .exe

phear-

  • Jr. Member
  • **
  • Posts: 64
    • MSN Messenger - LOApokalypse@hotmail.com
    • View Profile
    • http://gtproductions.org
Why?
« Reply #11 on: August 31, 2009, 08:33:15 pm »
Check for spelling errors and make sure you have all the files
Eugene Alfonso
GTP | Twitter

buggy123

  • Newbie
  • *
  • Posts: 26
    • View Profile
Why?
« Reply #12 on: September 01, 2009, 05:25:01 am »
well, the spellings correct. except for the "/" on the path it like data\art\menu\.. but if I try to do that it gives me a bunch of warnings about

warning C4129: 'm' : unrecognized character escape sequence

any thoughts?

RixCoder

  • Newbie
  • *
  • Posts: 40
    • View Profile
Why?
« Reply #13 on: September 01, 2009, 06:09:54 pm »
when you are using backslashes make sure to use '\\' instead of just a single '\'. Cause remember when you input a string like "Hello World\n" it will read that \n as a newline. You don't want it reading your file paths like that =p

the '\n' or any combo thereof like '\t' etc. are known as escape sequences.

buggy123

  • Newbie
  • *
  • Posts: 26
    • View Profile
Why?
« Reply #14 on: September 01, 2009, 09:56:38 pm »
yeh...i suspected it was that.

Anyways, thanks for the input guys. If this still doesn't work I'll just avoid all this trouble by taking the pictures out of the folders.