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

Author Topic: Win32 LoadFromFile for Image crashes  (Read 2336 times)

0 Members and 1 Guest are viewing this topic.

kamac

  • Newbie
  • *
  • Posts: 19
    • View Profile
Win32 LoadFromFile for Image crashes
« on: February 14, 2012, 05:44:22 pm »
I am trying to run Win32 app using SFML 1.6.

Here's the whole code:



Code: [Select]
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML\Graphics.hpp>
#include <Windows.h>


////////////////////////////////////////////////////////////
/// Function called whenever one of our windows receives a message
///
////////////////////////////////////////////////////////////
LRESULT CALLBACK OnEvent(HWND Handle, UINT Message, WPARAM WParam, LPARAM LParam)
{
    switch (Message)
    {
        // Quit when we close the main window
        case WM_CLOSE :
        {
            PostQuitMessage(0);
            return 0;
        }
    }

    return DefWindowProc(Handle, Message, WParam, LParam);
}


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \param Instance : Instance of the application
///
/// \return Error code
///
////////////////////////////////////////////////////////////
INT WINAPI WinMain(HINSTANCE Instance, HINSTANCE, LPSTR, INT)
{
    // Define a class for our main window
    WNDCLASS WindowClass;
    WindowClass.style         = 0;
    WindowClass.lpfnWndProc   = &OnEvent;
    WindowClass.cbClsExtra    = 0;
    WindowClass.cbWndExtra    = 0;
    WindowClass.hInstance     = Instance;
    WindowClass.hIcon         = NULL;
    WindowClass.hCursor       = 0;
    WindowClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BACKGROUND);
    WindowClass.lpszMenuName  = NULL;
    WindowClass.lpszClassName = TEXT("SFML App");
    RegisterClass(&WindowClass);

    // Let's create the main window
    HWND Window = CreateWindow(TEXT("SFML App"), TEXT("SFML Win32"), WS_SYSMENU | WS_VISIBLE, 0, 0, 800, 600, NULL, NULL, Instance, NULL);

    // Let's create two SFML views
    HWND View1 = CreateWindow(TEXT("STATIC"), NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 0,  0, 800, 600, Window, NULL, Instance, NULL);
    sf::RenderWindow SFMLView1(View1);

    // Load images to display
    sf::Image Image1;
    Image1.LoadFromFile("image.png");
    sf::Sprite Sprite1(Image1);
    Sprite1.SetPosition(55,105);

    // Loop until a WM_QUIT message is received
    MSG Message;
    Message.message = ~WM_QUIT;
    while (Message.message != WM_QUIT)
    {
        if (PeekMessage(&Message, NULL, 0, 0, PM_REMOVE))
        {
            // If a message was waiting in the message queue, process it
            TranslateMessage(&Message);
            DispatchMessage(&Message);
        }
        else
        {
   SFMLView1.Clear(sf::Color(0,0,0));
            // Draw sprite 1 on view 1
            SFMLView1.Draw(Sprite1);

            // Display each view on screen
            SFMLView1.Display();
        }
    }

    // Destroy the main window
    DestroyWindow(Window);

    // Don't forget to unregister the window class
    UnregisterClass(TEXT("SFML App"), Instance);

    return EXIT_SUCCESS;
}


Though, I get memory access violation on this line:
Code: [Select]
Image1.LoadFromFile("image.png");

What could possibly be wrong?

@EDIT

Nevermind me! I forgot to use Win32 Application templace instead of console.


Though, I have another question. Do you know any tutorials on how to programitically use edit boxes/combo boxes/buttons and so on?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Win32 LoadFromFile for Image crashes
« Reply #1 on: February 14, 2012, 08:18:20 pm »
Don't do that with the Win32 API, unless you're really forced to. There are nice high-level cross-platform APIs for GUIs.
Laurent Gomila - SFML developer

kamac

  • Newbie
  • *
  • Posts: 19
    • View Profile
Win32 LoadFromFile for Image crashes
« Reply #2 on: February 14, 2012, 09:34:20 pm »
Quote
There are nice high-level cross-platform APIs for GUIs


If you could give me their names out, I'd own you one  :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Win32 LoadFromFile for Image crashes
« Reply #3 on: February 14, 2012, 11:20:00 pm »
Qt, wxWidgets, GTK, FLTK.
Laurent Gomila - SFML developer