SFML community forums
Help => Graphics => Topic started by: windhl on August 22, 2007, 04:17:19 pm
-
now I am trying to test the code about Graphics
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
// Create the main rendering window
sfRenderWindow App(sfVideoMode(800, 600, 32), "SFML Graphics", false);
// Load the sprite image from a file
sfImage Image;
if (!Image.LoadFromFile("sprite.tga"))
return EXIT_FAILURE;
// Create the sprite
sfSprite Sprite(Image);
// Change its properties
Sprite.SetColor(sfColor(0, 255, 255, 128));
Sprite.SetLeft(200.f);
Sprite.SetTop(100.f);
Sprite.SetScale(2.f);
// Start game loop
bool Running = true;
while (Running)
{
// Process events
sfEvent Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sfEvent::Close)
Running = false;
}
// Get elapsed time
float ElapsedTime = App.GetFrameTime();
// Move the sprite
if (App.GetInput().IsKeyDown(sfKey::Left)) Sprite.SetLeft(Sprite.GetLeft() - 100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sfKey::Right)) Sprite.SetLeft(Sprite.GetLeft() + 100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sfKey::Up)) Sprite.SetTop(Sprite.GetTop() - 100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sfKey::Down)) Sprite.SetTop(Sprite.GetTop() + 100 * ElapsedTime);
// Rotate the sprite
if (App.GetInput().IsKeyDown(sfKey::Add)) Sprite.Rotate(- 100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sfKey::Subtract)) Sprite.Rotate(+ 100 * ElapsedTime);
// Display sprite in our window
App.Draw(Sprite);
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
I have added sfml-graphics.lib ,and I also added DevIL.dll, freetype6.dll and zlib1.dll in my project's directory. However, when I compile the code , there always are some problems:
error LNK2001: unresolved external symbol "public: bool __thiscall sfInput::IsKeyDown(enum sfKey::Code)const " (?IsKeyDown@sfInput@@QBE_NW4Code@sfKey@@@Z)
error LNK2001: unresolved external symbol "private: virtual void __thiscall sfWindow::OnEvent(class sfEvent const &)" (?OnEvent@sfWindow@@EAEXABVsfEvent@@@Z)
how can I do???
tks very much ~~~~ :)
-
You also need to link with sfml-window.lib and sfml-system.lib.
-
firstly , I use "Console App"
I compile successfully, but when I run the application, it runs wrong !!! :cry: :?:
Should I use "Win32 App"??
thank u~
-
what do you mean by runs wrong? it crashes?
-
code :
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
// Create the main rendering window
sfRenderWindow App(sfVideoMode(800, 600, 32), "SFML Graphics", false);
sfString Text("Hello SFML", "cheeseburger.ttf", 50);
bool Running = true;
while (Running)
{
// Process events
sfEvent Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sfEvent::Close)
Running = false;
}
App.Draw(Text);
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
I use F10 to trace the application, when it gos to
" sfString Text("Hello SFML", "cheeseburger.ttf", 50); "
there will be a message box with the Error:
Unhandled exception at 0x7c812a5b in sfml1.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012f45c.
why is it ??? :?:
-
Make sure you link to the "-d" versions in debug mode (sfml-graphics-d.lib, etc.), and also that you're using the multithreaded DLL CRT (in your project's options, C/C++, Code gneration, Runtime library).
... and Visual C++ 2005 of course :)