Win API with Lite-C Pure Mode

Posted By: amadeu

Win API with Lite-C Pure Mode - 09/02/07 17:06

Hallo

I am creating one game with lite-c pure mode.
I want to use windows.h creating Menu and Sub-menu in the game windows.

I create Menu and insert this menu with the code
long menu=CreateMenu();
InsertMenu(menu,1,MF_BYPOSITION|MF_STRING|MF_POPUP,hSubMenu1,"Option");
but when i will set the menu to the windos, did not work beocause I did not know the name of windows.
SetMenu(long hWnd,menu);

How i could create Menu and Submenu in Lite-c Pure?

thx
Posted By: dblade

Re: Win API with Lite-C Pure Mode - 09/02/07 17:39

Read the avars.h in the engine include folder you will get the handle of the engine window.
You can use all of the Vars in your programms (hope so).

hWnd is the HWND handle of the acknex engine.
hInstance is the HINSTANCE handle of the engine.

Menu adding works for me with hWnd

great feature

SetMenu(hWnd,YourMenu_here);
Posted By: amadeu

Re: Win API with Lite-C Pure Mode - 09/02/07 18:10

hI

Thanks, it worked.
I have other question. after I set my menu, I rum the game and when I try to click in the menu , nothing happens. Why? I created one function to create menu, as:
function createMenu()
{
long menu=CreateMenu();
long hSubMenu;
long hSubMenu1;
long hSubMenu2;
hSubMenu=CreateMenu();
hSubMenu1=CreateMenu();
hSubMenu2=CreateMenu();
InsertMenu(hSubMenu,1,MF_BYPOSITION|MF_STRING,1,"Open");
InsertMenu(hSubMenu1,0,MF_BYPOSITION|MF_STRING|MF_POPUP,hSubMenu2,"save");
InsertMenu(hSubMenu,0,MF_BYPOSITION|MF_STRING,3,"QUIT");
InsertMenu(menu,0,MF_BYPOSITION|MF_STRING|MF_POPUP,hSubMenu,"File");
InsertMenu(menu,1,MF_BYPOSITION|MF_STRING|MF_POPUP,hSubMenu1,"Option");
InsertMenu(hSubMenu2,0,MF_BYPOSITION|MF_STRING,1,"teste");
SetMenu(hWnd,menu);
}
What Will I need to do?
Posted By: amadeu

Re: Win API with Lite-C Pure Mode - 09/03/07 02:24

hI

Thanks, it worked.
I have other question. after I set my menu, I rum the game and when I try to click in the menu , nothing happens. Why? I created one function to create menu, as:
function createMenu()
{
long menu=CreateMenu();
long hSubMenu;
long hSubMenu1;
long hSubMenu2;
hSubMenu=CreateMenu();
hSubMenu1=CreateMenu();
hSubMenu2=CreateMenu();
InsertMenu(hSubMenu,1,MF_BYPOSITION|MF_STRING,1,"Open");
InsertMenu(hSubMenu1,0,MF_BYPOSITION|MF_STRING|MF_POPUP,hSubMenu2,"save");
InsertMenu(hSubMenu,0,MF_BYPOSITION|MF_STRING,3,"QUIT");
InsertMenu(menu,0,MF_BYPOSITION|MF_STRING|MF_POPUP,hSubMenu,"File");
InsertMenu(menu,1,MF_BYPOSITION|MF_STRING|MF_POPUP,hSubMenu1,"Option");
InsertMenu(hSubMenu2,0,MF_BYPOSITION|MF_STRING,1,"teste");
SetMenu(hWnd,menu);
}
What Will I need to do?
I need create the function: LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

I am lost. Could someone help me?

thx

Amadeu
Posted By: dblade

Re: Win API with Lite-C Pure Mode - 09/03/07 10:23

Have a Menu Problem, too
When mouse_sync is off the cursor isn`t visible over the added menu.
But when mouse_sync is on the cursor allways blinks.
(32 x 32) cursor image, tga

For your Problem:
Look at the mci.c example, there is a menu code
Copy it in an own Project (without WinMain and LRES stuff) and try.
Worked very well for me
Posted By: amadeu

Re: Win API with Lite-C Pure Mode - 09/03/07 10:50

Hi,
I tried but did not work. What I write when I take off the WinMain and LRES stuff?
I need create a new int function instead of WinMain like: int createMenu() and void WndProc(...) instead of LRESULT CALLBACK WndProc(...);
Please see my code above and tell me what I did wrong.


////////////////////////////////////////////////////////////////////
#include <litec.h>
#include <acknex.h>
#include <default.c>

////////////////////////////////////////////////////////////////////

void WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message){
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch(wParam){
case 1:
{
OPENFILENAME of;
char buf[256];
FillMemory(&of,sizeof(of),0);
FillMemory(buf,256,0);
of.Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST;
of.lpstrFilter ="Supported Files Types (*.mp3;*.wav;*.mpg;*.mpeg)\0*.mp3;*.wav;*.mpg;*.mpeg\0";
of.lStructSize=sizeof(of);
of.hwndOwner=hWnd;
of.lpstrFile=buf;
of.nMaxFile=255;
of.lpstrTitle="Open";
if (GetOpenFileName(&of))
MessageBox(NULL,"abriu","My first program",0);
}
break;
case 2:
break;
case 3:
PostQuitMessage(0);
break;
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hWnd, &ps);
RECT rect;GetClientRect(hWnd,&rect);
EndPaint(hWnd, &ps);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

int createMenu()
{
char *szClass="CScriptWindowClass";
HINSTANCE hInstance=GetModuleHandle(NULL);
UnregisterClass(szClass,hInstance);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW|CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance,128);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = COLOR_WINDOW+1;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szClass;
wcex.hIconSm = LoadIcon(hInstance,128);
RegisterClassEx(&wcex);
long menu=CreateMenu();
long hSubMenu;
hSubMenu=CreateMenu();
InsertMenu(hSubMenu,0,MF_BYPOSITION|MF_STRING,1,"Open");
InsertMenu(hSubMenu,2,MF_BYPOSITION|MF_STRING,3,"QUIT");
InsertMenu(menu,0,MF_BYPOSITION|MF_STRING|MF_POPUP,hSubMenu,"File");
if (hWnd)
{
SetMenu(hWnd,menu);
ShowWindow(hWnd,SW_SHOW);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
}



void main()
{
video_mode = 7; // lite-C: start in 640x480 resolution
video_screen = 2; // lite-C: start settings for Fullscreen
level_load("VAH.wmb");
wait(2); // let level load
Mouse_init(); // function for the mouse
createMenu();
}
Posted By: dblade

Re: Win API with Lite-C Pure Mode - 09/03/07 10:59

First, please tell me why did you inlude litec.h when you want to program in pure-mode ?
Just include acknex.h, windows.h and if you want default.c

Will check the code and post you an example, but will take a while (just re-installed GStudio)

See ya
Posted By: amadeu

Re: Win API with Lite-C Pure Mode - 09/03/07 11:11

ok
thanks.
i will wait your example.

See you.
Posted By: dblade

Re: Win API with Lite-C Pure Mode - 09/03/07 11:13

#include <acknex.h>
#include <windows.h>

int main(void)
{
wait(1); //don't set in the first frame
char *szClass="LiteC menu_test";
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW|CS_VREDRAW;
wcex.cbClsExtra = 0;
wcex.lpfnWndProc = NULL;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hbrBackground = COLOR_WINDOW+1;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szClass;
RegisterClassEx(&wcex);

long menu=CreateMenu();
long hSubMenu;
hSubMenu=CreateMenu();
InsertMenu(hSubMenu,0,MF_BYPOSITION|MF_STRING,1,"Open");
InsertMenu(hSubMenu,2,MF_BYPOSITION|MF_STRING,3,"QUIT");
InsertMenu(menu,0,MF_BYPOSITION|MF_STRING|MF_POPUP,hSubMenu,"File");

if(hWnd)
{
SetMenu(hWnd,menu);
}
}

/*
Here, a modified version of the mci menu
Works fine for me without the LRES stuff and so on
*/
Posted By: RedPhoenix

Re: Win API with Lite-C Pure Mode - 09/03/07 11:24

From MSDN:
Quote:

Associated with each menu item is a unique, application-defined integer, called a menu-item identifier. When the user chooses a command item from a menu, the system sends the item's identifier to the owner window as part of a WM_COMMAND message. The window procedure examines the identifier to determine the source of the message, and processes the message accordingly. In addition, you can specify a menu item using its identifier when you call menu functions; for example, to enable or disable a menu item.






This is your problem right? You can't get the message that the item sends when pressed by the user. I tried receiving this for days, but I didn't succeed what of course doesn't mean that it's impossible.
My advice: Create the menu on your own using panels and buttons. You can use the API for showing the menudialogs like open save and close, but I don't recommend you using the windows menustructure itself.
Posted By: dblade

Re: Win API with Lite-C Pure Mode - 09/03/07 11:32

Great Idea but if you want to create a big application with many menus you may have a lot of graphics and so on the design is one of the hardest parts (with GIMP).
With the WinAPI you have to write a lot of code but all looks clean then and you can use it with every resolution

Alternative create your images and use the great A6GUI created by BloodLine.
Posted By: amadeu

Re: Win API with Lite-C Pure Mode - 09/03/07 12:24

Hi.

thank you. it works.
But if I want to call one function when I click in Open or quit, How I could do this?
Posted By: dblade

Re: Win API with Lite-C Pure Mode - 09/03/07 14:59

Thats difficult in pure-mode i think
Will take a while to solfe this problem
I'll look for a way buut i can't promise anything maybe other users know something?

LiteC is a young language and noone except the creator knows all possibilitys
Posted By: RedPhoenix

Re: Win API with Lite-C Pure Mode - 09/03/07 15:39

Please look into my post, this is exactly what I was speaking of. When clicking on a menuitem in the menu, the API sends a Message to the engine window, but it's difficult to receive this Message without having the sourcecode of the hole engine I think, because you can't implement a message receiver for those messages that easy. In Legacy mode that could may be done, but at least I don't know how
Posted By: amadeu

Re: Win API with Lite-C Pure Mode - 09/03/07 17:39

hi

ok.

I will wait for your solution.

thanks
Posted By: jcl

Re: Win API with Lite-C Pure Mode - 09/05/07 09:06

You need to hook into the engine's message loop for retrieving messages. The ScanMessage pointer can be used for that. It's a pointer in the ev struct that you can set to your own message interpreter function.

http://manual.conitec.net/prog_sending.html
Posted By: RedPhoenix

Re: Win API with Lite-C Pure Mode - 09/05/07 09:07

Interesting I'll look into it, if I have success I'll post it here.
Posted By: amadeu

Re: Win API with Lite-C Pure Mode - 09/08/07 14:02

hi

Did you get it?

Thx
Posted By: yorisimo

Re: Win API with Lite-C Pure Mode - 09/18/07 19:15

Could you provide some more information about how to use ScanMessage. I've looked in the manual, at the Lite-C legacy examples, and the GStudio include files and I'm still having trouble. I have some questions about the engine window that is created by default when using Lite-C pure mode (I'll just call it Engine Window)
All my questions refer to Lite-C Pure mode.
1.Does the Engine Window automatically check for messages using GetMessage?
2.Is the Engine Window created using the WNDCLASS or WNDCLASSEX class and CreateWindow or CreateWindowEx?
3.Is the lpfnWndProc member of one of those classes set to ScanMessage by default?
Or do i need to use SetWindowLong, or some other Win API function to change the Window Procedure function?
4.I figured out I can use SetWindowLong(hWnd,GWL_STYLE, WS_CAPTION+WS_SYSMENU+WS_MINIMIZEBOX+WS_MAXIMIZEBOX+WS_SIZEBOX); ShowWindow(hWnd,SW_SHOW); to activate the maximize button and make the window sizeable. I also know how to get the current client area using GetClientRect() which I can use to adjust the resolution to mathch the resolution to the client area. However It seems video_set() affects window style, so the maximize button becomes inactive. What does video_set use to adjust the window resolution and why is it affecting the style?

I've included some code so it might be easier for you to find where I've made mistakes or if I'm totally on the wrong track. It runs, but nothing happens when you select the menu items, or click in the engine window. I put a beep in the ScanMessage function to see if it ever gets called--It doesn't!. So how do I use ScanMessage?! (Where/when/how does ScanMessage get called?)

Code:
 
#include <acknex.h>
#include <default.c>
#include <windows.h>

function ScanMessage(UINT message, WPARAM wParam, LPARAM lParam);

function main()
{
wait(1);

HMENU menu=CreateMenu();
HMENU hSubMenu=CreateMenu();
InsertMenu(hSubMenu,0,MF_BYPOSITION|MF_STRING,1,"BEEP");
InsertMenu(hSubMenu,1,MF_BYPOSITION|MF_STRING,2,"QUIT");
InsertMenu(menu,0,MF_BYPOSITION|MF_STRING|MF_POPUP,hSubMenu,"File");

//SetWindowLong(hWnd,GWL_WNDPROC, &ScanMessage); //(Causes Crash with or without '&')

//Change Window style to activate Maximize button
SetWindowLong(hWnd,GWL_STYLE, WS_CAPTION+WS_SYSMENU+WS_MINIMIZEBOX+WS_MAXIMIZEBOX+WS_SIZEBOX);
SetMenu(hWnd,menu);
ShowWindow(hWnd,SW_SHOW);

// I'm not sure if this part is already taken care of somewhere?
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);

wait(1);
}
///////////////////////////////
}

function ScanMessage(UINT message, WPARAM wParam, LPARAM lParam){
error("here");
switch(message){
case WM_LBUTTONDOWN:
MessageBox(NULL, "L mouse down","info", MB_OK);
break;
case WM_RBUTTONDOWN:
MessageBox(NULL, "R mouse down","info", MB_OK);
break;
case WM_COMMAND:
switch(wParam){
case 1: beep(); break;
case 2: sys_exit(NULL);
}
default:
MessageBox(NULL, "default","info", MB_OK);
}
return;
}


Posted By: yorisimo

Re: Win API with Lite-C Pure Mode - 09/20/07 15:05

Has anyone used ScanMessage successfully for tapping into the engine window message loop? Could you post some hints/examples of how to use it?
Posted By: RedPhoenix

Re: Win API with Lite-C Pure Mode - 09/20/07 15:23

I tried it very similar to your version, but like you had no success. The message function didn't react on any message (I tried several ones).
Posted By: yorisimo

Re: Win API with Lite-C Pure Mode - 09/20/07 15:29

bummer, I will email support
Posted By: yorisimo

Re: Win API with Lite-C Pure Mode - 09/20/07 17:38

Have not heard back from support yet obviously, but I figured out a solution without ScanMessage.

Try this example (should run fine):
Code:
 
#include <acknex.h>
#include <default.c>
#include <windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

function main()
{
wait(1);

HMENU menu=CreateMenu();
HMENU hSubMenu=CreateMenu();
InsertMenu(hSubMenu,0,MF_BYPOSITION|MF_STRING,1,"BEEP");
InsertMenu(hSubMenu,1,MF_BYPOSITION|MF_STRING,2,"QUIT");
InsertMenu(menu,0,MF_BYPOSITION|MF_STRING|MF_POPUP,hSubMenu,"File");

SetWindowLong(hWnd,GWL_WNDPROC, WndProc);

//Change Window style to activate Maximize button
SetWindowLong(hWnd,GWL_STYLE, WS_CAPTION+WS_SYSMENU+WS_MINIMIZEBOX+WS_MAXIMIZEBOX+WS_SIZEBOX);
SetMenu(hWnd,menu);
ShowWindow(hWnd,SW_SHOW);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
switch(message){
case WM_LBUTTONDOWN:
MessageBox(NULL, "L mouse down","info", MB_OK);
break;
case WM_RBUTTONDOWN:
MessageBox(NULL, "R mouse down","info", MB_OK);
break;
case WM_COMMAND:
switch(wParam){
case 1: beep(); break;
case 2: sys_exit(NULL);
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return(0);
}



I will make a nice example with some common dialogs and post in Lite-C contributions
Posted By: amadeu

Re: Win API with Lite-C Pure Mode - 09/22/07 08:23

Thx

it worked.
Posted By: TWO

Re: Win API with Lite-C Pure Mode - 09/22/07 16:42

This works for me:

Code:

LRESULT CALLBACK ScanMessage(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{

case WM_CLOSE:
sys_exit(NULL);

case WM_QUIT:
sys_exit(NULL);

case WM_KILLFOCUS:
ED_WindowFocus = false;

case WM_SETFOCUS:
ED_WindowFocus = true;

case WM_COMMAND:
{
switch(wParam)
{
case 1:
{
break;
}
case 2:
{
break;
}
case 3:
{
sys_exit(NULL);
break;
}
}
}

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}

return(0);
}



JCL, please add an example for this to the manual.
Posted By: yorisimo

Re: Win API with Lite-C Pure Mode - 09/25/07 16:07

I tried something like this but the function was never called. How is the ScanMessage function called?

I used "SetWindowLong(hWnd,GWL_WNDPROC, WndProc);" to set the window procedure to my function. I tried the same with ScanMessage but it didn't work because ScanMessage does not have the same format (no hWnd parameter)

I assume it's already set by default, but why isn't it being called then?
Posted By: yorisimo

Re: Win API with Lite-C Pure Mode - 09/26/07 18:18

@TWO,

I've noticed that my method prevents me from using all the key_... variables. I guess this is because they are set by the default Acknex window procedure, and I am not using the default window procedure.
Does your method of using ScanMessage cause the same problem or can you still use the key_... variables? I also still don't know how to use ScanMessage. (I understand the format of the callback function, but not how it gets called). Could you provide some advice?
Posted By: TWO

Re: Win API with Lite-C Pure Mode - 09/26/07 18:42

yorisimo, that's why the ScanMessage thing was implemented. So the engine can handle incoming messages and than pass them to the SM func.

Just define the function like I've done, it should work well.
Posted By: yorisimo

Re: Win API with Lite-C Pure Mode - 09/26/07 21:17

Could someone try this and let me know if it works. It's not working for me for some reason. When you resize (by dragging) the window it should give a message and beep. Thanks

Code:
 
#include <acknex.h>
#include <windows.h>

LRESULT CALLBACK ScanMessage(UINT message, WPARAM wParam, LPARAM lParam);

function main() {
level_load("box.wmb"); wait(-.5);
while(1){
wait(1);
}
}

LRESULT CALLBACK ScanMessage(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_SIZE:
error("window resized");
beep(); break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}

return(0);
}


Posted By: yorisimo

Re: Win API with Lite-C Pure Mode - 09/27/07 20:01

From GS email support:
"you must not name your function "ScanMessage" because
there's already a function pointer of this name (see manual). And you'll
need that pointer for implementing a message loop."
AND
"You must not have a default case, and not call DefWindowProc. Otherwise
the Windows Message loop is called twice for most messages, with
undesired effects."

@TWO: are you sure that your implementation of ScanMessage works? I noticed that most of your callbacks do default things (like exit)

How do you use the ScanMessage function pointer to implement the message loop?

I assumed it was something like this, but it's still not working (MyMessageFunction should cause a beep and error message when the window is resized):
Code:
 
#include <acknex.h>
#include <windows.h>

long MyMessageFunction(UINT message, WPARAM wParam, LPARAM lParam);

function main() {
ScanMessage=MyMessageFunction;
while(1) {
wait(1);
}
}

long MyMessageFunction(UINT message, WPARAM wParam, LPARAM lParam){
switch(message){
case WM_SIZE:
beep();
error("resized");
return(0);
}
}


Posted By: yorisimo

Win API with Lite-C Pure Mode - ScanMessage fix - 09/28/07 15:32

I got word back from GS support. ScanMessage was not setup for Lite-C, only for DLLs. This will be fixed in the next update along with an example in the manual. Until then, you'll need to edit avars.h like this:
Code:

#ifndef acknex_h //if using DLL
void (*SendPacket)(long to,void *data,long size,long flags); // the send function of the engine
void (*ReceivePacket)(long from,void *data,long size); // user provided receive function
long (*ScanMessage)(UINT message, WPARAM w_param, LPARAM l_param); // user provided message handler
#else // use normal pointers for lite-C
ENGINE_ void *SendPacket;
ENGINE_ void *ReceivePacket;
ENGINE_ void *ScanMessage;
#endif



and a working complete example of using it (resize the window and it will beep, all the defaults still work):
Code:

#include <acknex.h>
#include <windows.h>
#include <default.c>

function MyMessageFunction(UINT message, WPARAM wParam, LPARAM lParam);

function main() {
ScanMessage=MyMessageFunction;
}

function MyMessageFunction(UINT message, WPARAM wParam, LPARAM lParam){
if(message== WM_SIZE){ beep();}
}


Posted By: pararealist

Re: Win API with Lite-C Pure Mode - ScanMessage fix - 11/01/07 04:45

Since A7.06

#include <acknex.h>
#include <windows.h>
#include <default.c>

function MyMessageFunction(UINT message, WPARAM wParam, LPARAM lParam);

function main() {
on_scanmessage = MyMessageFunction;
}

function MyMessageFunction(UINT message, WPARAM wParam, LPARAM lParam){
if(message== WM_SIZE){ beep();}
}
Posted By: yorisimo

Re: Win API with Lite-C Pure Mode - ScanMessage fi - 11/09/07 16:51

yup, this thread was started before 7.06. This issue is now well documented.
© 2024 lite-C Forums