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

Author Topic: Using events with WinAPI.  (Read 4763 times)

0 Members and 1 Guest are viewing this topic.

blojayble

  • Newbie
  • *
  • Posts: 19
  • whoa
    • View Profile
Using events with WinAPI.
« on: October 31, 2011, 04:11:30 pm »
Hi all.


I have searched, but I still don't know it. Does events work if I use WinAPI like this:

http://www.sfml-dev.org/tutorials/1.6/graphics-win32.php  ?


If yes, how to do it? I tried by .GetEvent(Event) and

if( Event.Key.Code == sf::Key::A ) do something;

and it doesn't work. Maybe I placed it in wrong place...


I don't need a complete code,  just how to make it working. ;)



Thanks a lot for all answers.
0 bottles of beer on the wall,
0 bottles of beer!
Take one down, pass it around,
4,294,967,295 bottles of beer on the wall!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Using events with WinAPI.
« Reply #1 on: October 31, 2011, 04:20:46 pm »
We need to see your complete code ;)
Laurent Gomila - SFML developer

blojayble

  • Newbie
  • *
  • Posts: 19
  • whoa
    • View Profile
Using events with WinAPI.
« Reply #2 on: October 31, 2011, 04:28:44 pm »
The problem is, that I don't have it. :P

But I will write it now. :P


Code: [Select]
#include <windows.h>
#include <SFML\Graphics.hpp>


LPCWSTR NazwaKlasy = L"SFML App";

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
   
    // WYPEŁNIANIE STRUKTURY
    WNDCLASSEX wc;
   
    wc.cbSize = sizeof( WNDCLASSEX );
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground =( HBRUSH )( COLOR_WINDOW + 1 );
    wc.lpszMenuName = NULL;
    wc.lpszClassName = NazwaKlasy;
    wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
   
    // REJESTROWANIE KLASY OKNA
    if( !RegisterClassEx( & wc ) )
    {
        MessageBox( NULL, L"Wysoka Komisja odmawia rejestracji tego okna!", L"Niestety...",
        MB_ICONEXCLAMATION | MB_OK );
        return 1;
    }
   
    // TWORZENIE OKNA
    HWND hwnd;
   
    hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, NazwaKlasy, L"Oto okienko", WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL );
    HWND View = CreateWindowEx(NULL, NazwaKlasy,L"STATIC" ,WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,0,0,200,200,hwnd,NULL, hInstance,NULL);
    sf::RenderWindow SFMLView(View);




    if( hwnd == NULL )
    {
        MessageBox( NULL, L"Okno odmówiło przyjścia na świat!", L"Ale kicha...", MB_ICONEXCLAMATION );
        return 1;
}
   
    ShowWindow( hwnd, nCmdShow ); // Pokaż okienko...
    UpdateWindow( hwnd );
ShowWindow( View, nCmdShow ); // Pokaż okienko...
    UpdateWindow( View );
   
    // Pętla komunikatów
    MSG Message;
sf::Event Event;
   
while(SFMLView.GetEvent(Event))
{

if( Event.Key.Code == sf::Key::A ) SFMLView.Draw(sf::Shape::Circle(40,40,30,sf::Color::Green));


}


Message.message = static_cast<UINT>(~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
{
            // SFML
SFMLView.Clear(sf::Color::Yellow);

//SFMLView.Draw(sf::Shape::Circle(Message.pt.x,Message.pt.y,30,sf::Color::Cyan));
SFMLView.Draw(sf::Shape::Circle(20,20,30,sf::Color::Cyan));

SFMLView.Display();


     
        }
    }




    // Destroy the main window (all its child controls will be destroyed)
    DestroyWindow(hwnd);

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

}

// OBSŁUGA ZDARZEŃ
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
    case WM_CLOSE:
        DestroyWindow( hwnd );
        break;
       
    case WM_DESTROY:
        PostQuitMessage( 0 );
        break;
       
        default:
        return DefWindowProc( hwnd, msg, wParam, lParam );
    }
   
    return 0;
}




I know that what i done is stupid, but i have no idea what else can I do.
0 bottles of beer on the wall,
0 bottles of beer!
Take one down, pass it around,
4,294,967,295 bottles of beer on the wall!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Using events with WinAPI.
« Reply #3 on: October 31, 2011, 04:37:11 pm »
Try this:
Code: [Select]

   MSG Message;
   Message.message = static_cast<UINT>(~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
        {
           sf::Event Event;
           while (SFMLView.GetEvent(Event))
           {
               ...
           }  

           // SFML
           SFMLView.Clear(sf::Color::Yellow);
           ...
        }
    }
Laurent Gomila - SFML developer

blojayble

  • Newbie
  • *
  • Posts: 19
  • whoa
    • View Profile
Using events with WinAPI.
« Reply #4 on: October 31, 2011, 04:45:53 pm »
Still nothing.

I also tried like this:


Code: [Select]
MSG Message;

bool lol = false;


Message.message = static_cast<UINT>(~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
{
           


sf::Event Event;
         while(SFMLView.GetEvent(Event))
      {

      if( Event.Key.Code == sf::Key::A ) lol = true;


      }



// SFML
SFMLView.Clear(sf::Color::Yellow);

if(lol == true)SFMLView.Draw(sf::Shape::Circle(30,30,30,sf::Color::Blue));

//SFMLView.Draw(sf::Shape::Circle(Message.pt.x,Message.pt.y,30,sf::Color::Cyan));
SFMLView.Draw(sf::Shape::Circle(20,20,30,sf::Color::Cyan));

SFMLView.Display();


     
        }
    }




But it doesn't work too.

To be honest it work, but not when I press "A", but when i move mouse a lot. It's kinda bug.


/EDIT

I also found an issue while using Event.Key.Code == sf::something. When i move my mouse it can activate without pressing key I need. It happened  in other game too.
0 bottles of beer on the wall,
0 bottles of beer!
Take one down, pass it around,
4,294,967,295 bottles of beer on the wall!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Using events with WinAPI.
« Reply #5 on: October 31, 2011, 04:51:52 pm »
You must check the type of the event before checking its members. Using Event.Key.Code without knowing if the event is a KeyPressed or KeyReleased makes no sense.
Laurent Gomila - SFML developer

blojayble

  • Newbie
  • *
  • Posts: 19
  • whoa
    • View Profile
Using events with WinAPI.
« Reply #6 on: October 31, 2011, 05:02:34 pm »
OK, my bad.

Now I changed it to:

Code: [Select]
if( Event.Type == sf::Event::KeyPressed  && Event.Key.Code == sf::Key::A ) lol = true;


But it don't work at all.
0 bottles of beer on the wall,
0 bottles of beer!
Take one down, pass it around,
4,294,967,295 bottles of beer on the wall!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Using events with WinAPI.
« Reply #7 on: October 31, 2011, 06:16:21 pm »
Indeed. But it has nothing to do with SFML, if you try to catch events directly in your Win32 event callback, you'll see that they all happen in the owner window, not in the SFML child views.
Laurent Gomila - SFML developer

blojayble

  • Newbie
  • *
  • Posts: 19
  • whoa
    • View Profile
Using events with WinAPI.
« Reply #8 on: October 31, 2011, 06:58:46 pm »
So what I'm supposed to do?
0 bottles of beer on the wall,
0 bottles of beer!
Take one down, pass it around,
4,294,967,295 bottles of beer on the wall!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Using events with WinAPI.
« Reply #9 on: October 31, 2011, 08:21:11 pm »
I don't know. Would catching Win32 events on the main window be a solution? Otherwise, you'll have to find a way to enable keyboard and mouse events on child windows.
Laurent Gomila - SFML developer

blojayble

  • Newbie
  • *
  • Posts: 19
  • whoa
    • View Profile
Using events with WinAPI.
« Reply #10 on: October 31, 2011, 09:06:54 pm »
Hm, maybe it would be better to make my own controls.

But I don't exactly know how to do it...
0 bottles of beer on the wall,
0 bottles of beer!
Take one down, pass it around,
4,294,967,295 bottles of beer on the wall!

 

anything