Starting with Raylib

Started by Pakz, April 05, 2020, 17:50:57

Previous topic - Next topic

Aurel [banned]

Ok zelda
I finaly figured how to properly configure linker in ZinjaI IDE...well i like it ...
it is fast and lightweigth ..so do you can compile this program :

#include <windows.h>
LRESULT CALLBACK windowProcedure( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
//TCHAR text[] = _T"Hello world Window!";
//auto WinMessageToString(UINT msg) -> std::string;


int WINAPI WinMain(
   // Handle to current application isntance
   HINSTANCE hInstance,
   HINSTANCE hPrevInstance,
   // Command line
   LPSTR     lpCmdLine,
   int       nCmdShow
   ){

OutputDebugString("Starting WinMain Application");
//std::puts("It will not print to Console - Starting WinMain Application");   

//Window class name must be unique
const char wincClassName [] = "NameOfWindow";

// Win32 Window class structure
WNDCLASSEX wc;
// Win32 message structure
MSG Msg;           

// Name to identify the class with.
wc.lpszClassName = wincClassName;
//Pointer to the window procedure for this window class.
wc.lpfnWndProc = windowProcedure;   
// 1 - Register Windows Size
wc.cbSize = sizeof(WNDCLASSEX);

wc.style  = 0;
//Amount of extra data allocated for this class in memory. Usually 0
wc.cbClsExtra = 0;
//Amount of extra data allocated in memory per window of this type. Usually 0.
wc.cbWndExtra = 0;
//Handle to application instance (that we got in the first parameter of WinMain()).
wc.hInstance = hInstance;
// Large (usually 32x32) icon shown when the user presses Alt+Tab.
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
// Cursor that will be displayed over our window.
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
// Background Brush to set the color of our window.
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // (HBRUSH) CreateSolidBrush(RGB(10, 20, 30)); //
// Background Brush to set the color of our window.
wc.lpszMenuName = NULL;
// Small (usually 16x16) icon to show in the taskbar and in the top left corner of the window.
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 

//OutputDebugString("Registered Window Class OK.");

if(!RegisterClassEx(&wc))
{
MessageBox( 0,
   "Window Registration Failed!",
   "Error!",
   MB_ICONEXCLAMATION | MB_OK);
//Error status code
return -1;
}

//std::cout << "Class Registered" << std::endl;

int width = 500, height = 400;
int pos_left = 400, pos_top = 100;

HWND hwnd = CreateWindowA(
  wc.lpszClassName,
  "Title of Window",
  WS_OVERLAPPEDWINDOW,
  pos_left,
  pos_top,
  width,
  height,
  0,
  0,
  hInstance,
  0
  ); 
OutputDebugString(" [INFO] Window created OK");

if(hwnd == NULL){
MessageBox(NULL,
   "Error: Failure to create Window",
   "Error Report",
   MB_ICONEXCLAMATION | MB_OK);
return -1;
}
ShowWindow(hwnd, nCmdShow);
//std::cout << "nCmdShow = " << nCmdShow << std::endl;
UpdateWindow(hwnd);

//---- Message Loop ----------//
while(GetMessage(&Msg, NULL, 0, 0) > 0 ){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

OutputDebugString(" [INFO] Exiting application. OK."); 
// Success status code
return 0;
}
// ------------ End of Main ------------------------- //


// Window Procedure - Process window messages or events
LRESULT CALLBACK windowProcedure (
  HWND   hwnd    // Window Handle (Window object)
  ,UINT   msg     // Window Message 
  ,WPARAM wParam  // Additional message information
  ,LPARAM lParam  // Additional message information
  )
// Ignore messages which can flood the logging
// if(msg != WM_MOUSEMOVE && msg != WM_NCMOUSEMOVE
//    && msg != WM_QUIT && msg != WM_NCHITTEST )
//if(ignored_messages.find(msg) == ignored_messages.end())
// OutputDebugString(WinMessageToString(msg).c_str());
{
// Process messages
switch(msg)
{
case WM_CREATE:
SetWindowTextA(hwnd, "Window Title - GDI draw by Aurel");       
// OutputDebugString(" [INFO] Window created. OK.");
break;
case WM_CLOSE:
//OutputDebugString(" [INFO] Window closed. OK.");
DestroyWindow(hwnd);
break;
case WM_DESTROY:
//OutputDebugString(" [INFO] Exiting application. OK.");
PostQuitMessage(0);
break;
case WM_MOVE:
//std::cerr << " [INFO] Move window." << std::endl;
break;
case WM_PAINT:
{
// GDI - Graphics Devices Interface Here
//--------------------------------------------
PAINTSTRUCT ps;
HDC hdc;
// std::cerr << " [INFO] Windown painting" << std::endl;
hdc = BeginPaint(hwnd, &ps);
//TCHAR greeting[] = _T("Hello, World!");
const char text[] = "Hello world Window!";       
TextOutA(hdc, 125, 200, text , 19);
Ellipse(hdc, 200, 200, 160, 160);
Rectangle(hdc, 100, 100, 160, 160);
EndPaint(hwnd, &ps);       
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}

return 0;
}


(Y)

zelda64bit

Quote from: Aurel on June 06, 2021, 11:12:59
Ok zelda
I finaly figured how to properly configure linker in ZinjaI IDE...well i like it ...
it is fast and lightweigth ..so do you can compile this program :

#include <windows.h>
LRESULT CALLBACK windowProcedure( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
//TCHAR text[] = _T"Hello world Window!";
//auto WinMessageToString(UINT msg) -> std::string;


int WINAPI WinMain(
   // Handle to current application isntance
   HINSTANCE hInstance,
   HINSTANCE hPrevInstance,
   // Command line
   LPSTR     lpCmdLine,
   int       nCmdShow
   ){

OutputDebugString("Starting WinMain Application");
//std::puts("It will not print to Console - Starting WinMain Application");   

//Window class name must be unique
const char wincClassName [] = "NameOfWindow";

// Win32 Window class structure
WNDCLASSEX wc;
// Win32 message structure
MSG Msg;           

// Name to identify the class with.
wc.lpszClassName = wincClassName;
//Pointer to the window procedure for this window class.
wc.lpfnWndProc = windowProcedure;   
// 1 - Register Windows Size
wc.cbSize = sizeof(WNDCLASSEX);

wc.style  = 0;
//Amount of extra data allocated for this class in memory. Usually 0
wc.cbClsExtra = 0;
//Amount of extra data allocated in memory per window of this type. Usually 0.
wc.cbWndExtra = 0;
//Handle to application instance (that we got in the first parameter of WinMain()).
wc.hInstance = hInstance;
// Large (usually 32x32) icon shown when the user presses Alt+Tab.
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
// Cursor that will be displayed over our window.
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
// Background Brush to set the color of our window.
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // (HBRUSH) CreateSolidBrush(RGB(10, 20, 30)); //
// Background Brush to set the color of our window.
wc.lpszMenuName = NULL;
// Small (usually 16x16) icon to show in the taskbar and in the top left corner of the window.
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 

//OutputDebugString("Registered Window Class OK.");

if(!RegisterClassEx(&wc))
{
MessageBox( 0,
   "Window Registration Failed!",
   "Error!",
   MB_ICONEXCLAMATION | MB_OK);
//Error status code
return -1;
}

//std::cout << "Class Registered" << std::endl;

int width = 500, height = 400;
int pos_left = 400, pos_top = 100;

HWND hwnd = CreateWindowA(
  wc.lpszClassName,
  "Title of Window",
  WS_OVERLAPPEDWINDOW,
  pos_left,
  pos_top,
  width,
  height,
  0,
  0,
  hInstance,
  0
  ); 
OutputDebugString(" [INFO] Window created OK");

if(hwnd == NULL){
MessageBox(NULL,
   "Error: Failure to create Window",
   "Error Report",
   MB_ICONEXCLAMATION | MB_OK);
return -1;
}
ShowWindow(hwnd, nCmdShow);
//std::cout << "nCmdShow = " << nCmdShow << std::endl;
UpdateWindow(hwnd);

//---- Message Loop ----------//
while(GetMessage(&Msg, NULL, 0, 0) > 0 ){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

OutputDebugString(" [INFO] Exiting application. OK."); 
// Success status code
return 0;
}
// ------------ End of Main ------------------------- //


// Window Procedure - Process window messages or events
LRESULT CALLBACK windowProcedure (
  HWND   hwnd    // Window Handle (Window object)
  ,UINT   msg     // Window Message 
  ,WPARAM wParam  // Additional message information
  ,LPARAM lParam  // Additional message information
  )
// Ignore messages which can flood the logging
// if(msg != WM_MOUSEMOVE && msg != WM_NCMOUSEMOVE
//    && msg != WM_QUIT && msg != WM_NCHITTEST )
//if(ignored_messages.find(msg) == ignored_messages.end())
// OutputDebugString(WinMessageToString(msg).c_str());
{
// Process messages
switch(msg)
{
case WM_CREATE:
SetWindowTextA(hwnd, "Window Title - GDI draw by Aurel");       
// OutputDebugString(" [INFO] Window created. OK.");
break;
case WM_CLOSE:
//OutputDebugString(" [INFO] Window closed. OK.");
DestroyWindow(hwnd);
break;
case WM_DESTROY:
//OutputDebugString(" [INFO] Exiting application. OK.");
PostQuitMessage(0);
break;
case WM_MOVE:
//std::cerr << " [INFO] Move window." << std::endl;
break;
case WM_PAINT:
{
// GDI - Graphics Devices Interface Here
//--------------------------------------------
PAINTSTRUCT ps;
HDC hdc;
// std::cerr << " [INFO] Windown painting" << std::endl;
hdc = BeginPaint(hwnd, &ps);
//TCHAR greeting[] = _T("Hello, World!");
const char text[] = "Hello world Window!";       
TextOutA(hdc, 125, 200, text , 19);
Ellipse(hdc, 200, 200, 160, 160);
Rectangle(hdc, 100, 100, 160, 160);
EndPaint(hwnd, &ps);       
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}

return 0;
}




What is that code for?

iWasAdam

pretty standard c/c++ code for opening a window with a base message map.

What it really shows is that underneath all the nice basic 'OpenWindow' commands is some knarly low level stuff going on. you need window classes, icons, message loops, callbacks, etc

Aurel [banned]

yes it is small window C app it shows primitive GDI drawing
but instead primitive you can create sprites too.
(Y)