SFML community forums
Help => Window => Topic started 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:
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?
-
Can you post a complete code? Why are you talking about Win API?
-
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]
-
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.
-
But winapi input sucks :/ Anyway to get this input? I need pointer to render window...
-
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?
-
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...
-
If you give a complete source code I can try it and see what's wrong.
-
But it's complete... There is only functions ex. displaying fps on window caption etc.
#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());
}
-
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 ;)).
-
Here is main.h
#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
#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:
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).
-
This example works fine for me, the mouse coordinates are updated properly.
-
But u checking mouse coords from winapi or SFML? To see pos from SFML switch comments in fuction Display_FPS(this line with sprintf).
-
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 ;)
-
It is!
int Mouse_X = Render->GetInput().GetMouseX(), Mouse_Y = Render->GetInput().GetMouseY();
-
Ah, sorry :shock:
I guess I commented the wrong line :lol: