SFML community forums

Help => Window => Topic started by: ShadowDancer on October 24, 2009, 02:06:15 pm

Title: Input with winapi?
Post by: ShadowDancer on October 24, 2009, 02:06:15 pm
I have question:
Is possible to take mouse input via render window, when i'm using Winapi?
I'm currently doing it like this:
Code: [Select]

sprintf( Buffer, " MouseX: %d MouseY: %d", Render->GetInput().GetMouseX(), Render->GetInput().GetMouseY() );


And MouseX/Y is 0. Winapi changes smthing? I should take mouse from view or what?
Title: Input with winapi?
Post by: Laurent on October 24, 2009, 05:53:03 pm
Can you post a complete code? Why are you talking about Win API?
Title: Input with winapi?
Post by: ShadowDancer on October 24, 2009, 06:08:04 pm
Code: [Select]
sf::RenderWindow *Render;
   HWND hWindow, Render_Window;
   MSG Msg;

main()
{
   WNDCLASSEX KlasaOkna;
   ZeroMemory (&KlasaOkna, sizeof(WNDCLASSEX));
   KlasaOkna.cbSize = sizeof(WNDCLASSEX);
   KlasaOkna.hInstance = hInstance;
   KlasaOkna.lpfnWndProc = WindowEventProc;
   KlasaOkna.lpszClassName = WindowClass.c_str();
   KlasaOkna.hCursor = LoadCursor(NULL, IDC_ARROW);
   KlasaOkna.hIcon = LoadIcon(NULL, IDI_APPLICATION);
   KlasaOkna.hbrBackground = (HBRUSH) COLOR_WINDOW;
   RegisterClassEx (&KlasaOkna);
   // tworzymy okno funkcją CreateWindowEx
   hWindow = CreateWindowEx(NULL,                   // rozszerzony styl
                          WindowClass.c_str(), // klasa okna
                          WindowCaption.c_str(),       // tekst na p. tytułu
                          WS_SYSMENU,   // styl okna
                          CW_USEDEFAULT,         // współrzędna X
                          CW_USEDEFAULT,         // współrzędna Y
                          800,         // szerokość
                          600,         // wysokość
                          NULL,                  // okno nadrzędne
                          NULL,                  // menu
                          hInstance,             // instancjs aplikacji
                          NULL);                 // dodatkowe dane
DWORD Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS;
HWND  Render_Window = CreateWindow("STATIC", NULL, Style, 0,  0, 800, 600, hWindow, NULL, hInstance, NULL);
sf::RenderWindow View01(Render_Window);
Render = &View01;
ShowWindow (hWindow, SW_SHOW);

while (Msg.message != WM_QUIT)
{
   if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
   {
         TranslateMessage (&Msg);
         DispatchMessage (&Msg);
   }
   else
   {
    sf::Event Event;
    while (Render->GetEvent(Event))
    {
        // Process event
    }
Display_FPS();
float32 timeStep = 1.0f / 60.0f;
int32 iterations = 10;
B2DWorld->Step(timeStep, iterations);

int Mouse_X = Render->GetInput().GetMouseX(), Mouse_Y = Render->GetInput().GetMouseY();
Render->Clear(sf::Color(255, 255, 255));

Button.Display(Render, DeltaTime);

Render->Display();
   }
}
[/code]
Title: Input with winapi?
Post by: Laurent on October 24, 2009, 06:36:58 pm
I don't know why you're using Win API directly, but if you do so then yes you can handle input directly as well.
Title: Input with winapi?
Post by: ShadowDancer on October 24, 2009, 07:47:56 pm
But winapi input sucks :/ Anyway to get this input? I need pointer to render window...
Title: Input with winapi?
Post by: Nexus on October 24, 2009, 08:00:25 pm
Quote from: "ShadowDancer"
Anyway to get this input? I need pointer to render window...
What about sf::RenderWindow::GetEvent() or sf::RenderWindow::GetInput()? Input handling is explained in the tutorials, by the way...

Or what is exactly your problem?
Title: Re: Input with winapi?
Post by: ShadowDancer on October 24, 2009, 08:08:43 pm
Quote from: "ShadowDancer"

Is possible to take mouse input via render window, when i'm using Winapi?

[...]

And MouseX/Y is 0. Winapi changes smthing? I should take mouse from view or what?


Just in RenderWindow instance values myMouseX and myMouseY is 0, idk why...
Title: Input with winapi?
Post by: Laurent on October 24, 2009, 08:38:33 pm
If you give a complete source code I can try it and see what's wrong.
Title: Input with winapi?
Post by: ShadowDancer on October 24, 2009, 10:00:30 pm
But it's complete... There is only functions ex. displaying fps on window caption etc.

Code: [Select]

#include "Main.h"
#define WIN32_LEAN_AND_MEAN
sf::RenderWindow *Render;
int mouse_x, mouse_y;

//FPS and Delta Time
    int FPS_Frames;
    float DeltaTime;
    sf::Clock FPS_Timer;
float FPS_Number, FPS_Time;

int FPS;
//zmienne okienka
   HWND hWindow, Render_Window;
   MSG Msg;
// nazwa klasy okna
   std::string WindowClass = "Window";
   std::string WindowCaption = " ";

b2AABB WorldSize;

b2Vec2 gravity(0.0f, 0.0f);
bool doSleep = true;

//zasoby

//obrazki
sf::Image Img_Button[2];

//fonty
sf::Font Fnt_Arial, Fnt_Times_New_Roman;

void Display_FPS();
LRESULT CALLBACK WindowEventProc(HWND hWindow, UINT uMsg,WPARAM wParam, LPARAM lParam);
//----------------------- funkcja WinMain()----------------------------

 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)

{


   WNDCLASSEX KlasaOkna;
   ZeroMemory (&KlasaOkna, sizeof(WNDCLASSEX));
   KlasaOkna.cbSize = sizeof(WNDCLASSEX);
   KlasaOkna.hInstance = hInstance;
   KlasaOkna.lpfnWndProc = WindowEventProc;
   KlasaOkna.lpszClassName = WindowClass.c_str();
   KlasaOkna.hCursor = LoadCursor(NULL, IDC_ARROW);
   KlasaOkna.hIcon = LoadIcon(NULL, IDI_APPLICATION);
   KlasaOkna.hbrBackground = (HBRUSH) COLOR_WINDOW;
   RegisterClassEx (&KlasaOkna);

   // tworzymy okno funkcją CreateWindowEx
   hWindow = CreateWindowEx(NULL,                   // rozszerzony styl
                          WindowClass.c_str(), // klasa okna
                          WindowCaption.c_str(),       // tekst na p. tytułu
                          WS_SYSMENU,   // styl okna
                          CW_USEDEFAULT,         // współrzędna X
                          CW_USEDEFAULT,         // współrzędna Y
                          800,         // szerokość
                          600,         // wysokość
                          NULL,                  // okno nadrzędne
                          NULL,                  // menu
                          hInstance,             // instancjs aplikacji
                          NULL);                 // dodatkowe dane

   // pokazujemy nasze okno

DWORD Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS;
HWND  Render_Window = CreateWindow("STATIC", NULL, Style, 0,  0, 800, 600, hWindow, NULL, hInstance, NULL);
sf::RenderWindow View01(Render_Window);
Render = &View01;
ShowWindow (hWindow, SW_SHOW);

while (Msg.message != WM_QUIT)
{
   if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
   {
         TranslateMessage (&Msg);
         DispatchMessage (&Msg);
   }
   else
   {
    sf::Event Event;
    while (Render->GetEvent(Event))
    {
        // Process event
    }



Display_FPS();


Render->Clear(sf::Color(255, 255, 255));
//some display functions
Button.Display(Render, DeltaTime);

Render->Display();
   }
}

return static_cast<int>(Msg.wParam);// zwracamy kod wyjścia
}
LRESULT CALLBACK WindowEventProc(HWND hWindow, UINT uMsg,WPARAM wParam, LPARAM lParam)
{
   switch (uMsg)
   {
      case WM_CREATE:
      break;
      case WM_DESTROY: // kończymy program
               PostQuitMessage (0);
               return 0;
      break;
//mouse move by winapi
 case WM_MOUSEMOVE:
      {
 mouse_x = LOWORD(lParam);
                      mouse_y = HIWORD(lParam);
      }
 break;
   }
   return DefWindowProc(hWindow, uMsg, wParam, lParam);
}
void Display_FPS()
{
   //delta time calculate
      DeltaTime = FPS_Timer.GetElapsedTime();
  FPS_Timer.Reset();
   //FPS calculate
   ++FPS_Frames;//add frame
FPS_Time += DeltaTime;//time of generating one frame
if (FPS_Time >= 1.0f)   // czy minęła sekunda?
{
 FPS_Number = FPS_Frames / FPS_Time;//calculate fps
 FPS_Frames = 0;//reset frame number
 FPS_Time = 0.0f;//reset time
}

std::string tmp, tmp2, tmp3; // brzydkie rozwiązanie
itoa((int)FPS_Number, (char*)tmp.c_str(), 10);

std::string Caption;
Caption += tmp.c_str();
char Buffer[255];
int Mouse_X = Render->GetInput().GetMouseX(), Mouse_Y = Render->GetInput().GetMouseY();
//sprintf( Buffer, " MouseX: %d MouseY: %d", Mouse_X, Mouse_Y); - currently using Winapi input
sprintf( Buffer, " MouseX: %d MouseY: %d Button %d", mouse_x, mouse_y, Button.State);
Caption += Buffer;
SetWindowText(hWindow, Caption.c_str());
}
Title: Input with winapi?
Post by: Laurent on October 24, 2009, 10:13:45 pm
Quote
But it's complete

No it's not. I can't compile it (yes, I want to test it with a real compiler, not only with my eyes ;)).
Title: Input with winapi?
Post by: ShadowDancer on October 24, 2009, 11:15:05 pm
Here is main.h
Code: [Select]

#ifndef Main_H
#define Main_H

#include <vector>
#include <windows.h>

//SFML graphics
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

//main values
extern sf::RenderWindow *Render;

#endif


Main.cpp
Code: [Select]

#include "Main.h"

#define WIN32_LEAN_AND_MEAN
#pragma warning (disable : 4996)

int mouse_x, mouse_y;

sf::RenderWindow *Render;



//FPS and Delta Time
    int FPS_Frames;
    float DeltaTime;
    sf::Clock FPS_Timer;
float FPS_Number, FPS_Time;

int FPS;
//zmienne okienka
   HWND hWindow, Render_Window;
   MSG Msg;
// nazwa klasy okna
   std::string WindowClass = "Window";
   std::string WindowCaption = "Caption";



void Display_FPS();
LRESULT CALLBACK WindowEventProc(HWND hWindow, UINT uMsg,WPARAM wParam, LPARAM lParam);
//----------------------- funkcja WinMain()----------------------------

 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)

{

   WNDCLASSEX KlasaOkna;
   ZeroMemory (&KlasaOkna, sizeof(WNDCLASSEX));
   KlasaOkna.cbSize = sizeof(WNDCLASSEX);
   KlasaOkna.hInstance = hInstance;
   KlasaOkna.lpfnWndProc = WindowEventProc;
   KlasaOkna.lpszClassName = WindowClass.c_str();
   KlasaOkna.hCursor = LoadCursor(NULL, IDC_ARROW);
   KlasaOkna.hIcon = LoadIcon(NULL, IDI_APPLICATION);
   KlasaOkna.hbrBackground = (HBRUSH) COLOR_WINDOW;
   RegisterClassEx (&KlasaOkna);

   // tworzymy okno funkcją CreateWindowEx
   hWindow = CreateWindowEx(NULL,                   // rozszerzony styl
                          WindowClass.c_str(), // klasa okna
                          WindowCaption.c_str(),       // tekst na p. tytułu
                          WS_SYSMENU,   // styl okna
                          CW_USEDEFAULT,         // współrzędna X
                          CW_USEDEFAULT,         // współrzędna Y
                          800,         // szerokość
                          600,         // wysokość
                          NULL,                  // okno nadrzędne
                          NULL,                  // menu
                          hInstance,             // instancjs aplikacji
                          NULL);                 // dodatkowe dane

   // pokazujemy nasze okno

DWORD Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS;
HWND  Render_Window = CreateWindow("STATIC", NULL, Style, 0,  0, 800, 600, hWindow, NULL, hInstance, NULL);
sf::RenderWindow View01(Render_Window);
Render = &View01;
ShowWindow (hWindow, SW_SHOW);


while (Msg.message != WM_QUIT)
{
   if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
   {
         TranslateMessage (&Msg);
         DispatchMessage (&Msg);
   }
   else
   {
    sf::Event Event;
    while (Render->GetEvent(Event))
    {
        // Process event
    }



Display_FPS();


Render->Clear(sf::Color(255, 255, 255));


Render->Display();
   }
}

return static_cast<int>(Msg.wParam);// zwracamy kod wyjścia
}
LRESULT CALLBACK WindowEventProc(HWND hWindow, UINT uMsg,WPARAM wParam, LPARAM lParam)
{
   switch (uMsg)
   {
      case WM_CREATE:
      break;
      case WM_DESTROY: // kończymy program
               PostQuitMessage (0);
               return 0;
      break;
 case WM_MOUSEMOVE:
      {
 mouse_x = LOWORD(lParam);
                      mouse_y = HIWORD(lParam);
      }
break;
   }
   return DefWindowProc(hWindow, uMsg, wParam, lParam);
}

void Display_FPS()
{
   //delta time calculate
      DeltaTime = FPS_Timer.GetElapsedTime();
  FPS_Timer.Reset();
   //FPS calculate
   ++FPS_Frames;//add frame
FPS_Time += DeltaTime;//time of generating one frame
if (FPS_Time >= 1.0f)   // czy minęła sekunda?
{
 FPS_Number = FPS_Frames / FPS_Time;//calculate fps
 FPS_Frames = 0;//reset frame number
 FPS_Time = 0.0f;//reset time
}

std::string tmp, tmp2, tmp3; // brzydkie rozwiązanie
itoa((int)FPS_Number, (char*)tmp.c_str(), 10);

std::string Caption = "Caption ";
Caption += tmp.c_str();
char Buffer[255];
int Mouse_X = Render->GetInput().GetMouseX(), Mouse_Y = Render->GetInput().GetMouseY();
//sprintf( Buffer, " MouseX: %d MouseY: %d", Mouse_X, Mouse_Y);
sprintf( Buffer, " MouseX: %d MouseY: %d", mouse_x, mouse_y);
Caption += Buffer;
SetWindowText(hWindow, Caption.c_str());
}


And linker input:
Code: [Select]

sfml-system-s-d.lib sfml-main-d.lib sfml-window-s-d.lib sfml-graphics-s-d.lib

Project is much bigger i cut object etc classes, and other not important things(it's commercial and not only my project, so i can't paste here all).
Title: Input with winapi?
Post by: Laurent on October 25, 2009, 10:51:22 am
This example works fine for me, the mouse coordinates are updated properly.
Title: Input with winapi?
Post by: ShadowDancer on October 25, 2009, 11:37:44 am
But u checking mouse coords from winapi or SFML? To see pos from SFML switch comments in fuction Display_FPS(this line with sprintf).
Title: Input with winapi?
Post by: Laurent on October 25, 2009, 12:21:32 pm
Quote
But u checking mouse coords from winapi or SFML? To see pos from SFML switch comments in fuction Display_FPS(this line with sprintf).

Ok I see.
It switched to SFML but now it doesn't compile (Mouse_X and Mouse_Y are not declared).

By the way, did you check the Win32 sample in the SDK? If this one works then you have a problem, if not then I have a problem ;)
Title: Input with winapi?
Post by: ShadowDancer on October 25, 2009, 01:18:04 pm
It is!

Code: [Select]
int Mouse_X = Render->GetInput().GetMouseX(), Mouse_Y = Render->GetInput().GetMouseY();
Title: Input with winapi?
Post by: Laurent on October 25, 2009, 07:29:03 pm
Ah, sorry :shock:

I guess I commented the wrong line :lol: