[ win32 ][ code::block's ] game 1 error ?
-
hello this is a try at a verry simple game it is a try at moving a circle whit cursor-key's i use code::block's 4 error's were detected : somting wrong whit case any help is welkome
// bluatigro 23 okt 2017
// win game 1#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif#include #include bool key[ 256 ] ;
int playerx , playery , winx , winy ;
#define FRAME_TIMER 1/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);/* Make the class name into a global variable */
= _T("CodeBlocksWindowsApp");int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass *//\* The Window structure \*/ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /\* This function is called by windows \*/ wincl.style = CS\_DBLCLKS; /\* Catch double-clicks \*/ wincl.cbSize = sizeof (WNDCLASSEX); /\* Use default icon and mouse-pointer \*/ wincl.hIcon = LoadIcon (NULL, IDI\_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI\_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC\_ARROW); wincl.lpszMenuName = NULL; /\* No menu \*/ wincl.cbClsExtra = 0; /\* No extra bytes after the window class \*/ wincl.cbWndExtra = 0; /\* structure or the window instance \*/ /\* Use Windows's default colour as the background of the window \*/ wincl.hbrBackground = (HBRUSH) COLOR\_BACKGROUND; /\* Register the window class, and if it fails quit the program \*/ if (!RegisterClassEx (&wincl)) return 0; /\* The class is registered, let's create the program\*/ hwnd = CreateWindowEx ( 0, /\* Extended possibilites for variation \*/ szClassName, /\* Classname \*/ \_T("Game 1"), /\* Title Text \*/ WS\_OVERLAPPEDWINDOW | WS\_MAXIMIZE , /\* default window \*/ CW\_USEDEFAULT, /\* Windows decides the position \*/ CW\_USEDEFAULT,
-
hello this is a try at a verry simple game it is a try at moving a circle whit cursor-key's i use code::block's 4 error's were detected : somting wrong whit case any help is welkome
// bluatigro 23 okt 2017
// win game 1#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif#include #include bool key[ 256 ] ;
int playerx , playery , winx , winy ;
#define FRAME_TIMER 1/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);/* Make the class name into a global variable */
= _T("CodeBlocksWindowsApp");int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass *//\* The Window structure \*/ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /\* This function is called by windows \*/ wincl.style = CS\_DBLCLKS; /\* Catch double-clicks \*/ wincl.cbSize = sizeof (WNDCLASSEX); /\* Use default icon and mouse-pointer \*/ wincl.hIcon = LoadIcon (NULL, IDI\_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI\_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC\_ARROW); wincl.lpszMenuName = NULL; /\* No menu \*/ wincl.cbClsExtra = 0; /\* No extra bytes after the window class \*/ wincl.cbWndExtra = 0; /\* structure or the window instance \*/ /\* Use Windows's default colour as the background of the window \*/ wincl.hbrBackground = (HBRUSH) COLOR\_BACKGROUND; /\* Register the window class, and if it fails quit the program \*/ if (!RegisterClassEx (&wincl)) return 0; /\* The class is registered, let's create the program\*/ hwnd = CreateWindowEx ( 0, /\* Extended possibilites for variation \*/ szClassName, /\* Classname \*/ \_T("Game 1"), /\* Title Text \*/ WS\_OVERLAPPEDWINDOW | WS\_MAXIMIZE , /\* default window \*/ CW\_USEDEFAULT, /\* Windows decides the position \*/ CW\_USEDEFAULT,
-
hello this is a try at a verry simple game it is a try at moving a circle whit cursor-key's i use code::block's 4 error's were detected : somting wrong whit case any help is welkome
// bluatigro 23 okt 2017
// win game 1#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif#include #include bool key[ 256 ] ;
int playerx , playery , winx , winy ;
#define FRAME_TIMER 1/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);/* Make the class name into a global variable */
= _T("CodeBlocksWindowsApp");int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass *//\* The Window structure \*/ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /\* This function is called by windows \*/ wincl.style = CS\_DBLCLKS; /\* Catch double-clicks \*/ wincl.cbSize = sizeof (WNDCLASSEX); /\* Use default icon and mouse-pointer \*/ wincl.hIcon = LoadIcon (NULL, IDI\_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI\_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC\_ARROW); wincl.lpszMenuName = NULL; /\* No menu \*/ wincl.cbClsExtra = 0; /\* No extra bytes after the window class \*/ wincl.cbWndExtra = 0; /\* structure or the window instance \*/ /\* Use Windows's default colour as the background of the window \*/ wincl.hbrBackground = (HBRUSH) COLOR\_BACKGROUND; /\* Register the window class, and if it fails quit the program \*/ if (!RegisterClassEx (&wincl)) return 0; /\* The class is registered, let's create the program\*/ hwnd = CreateWindowEx ( 0, /\* Extended possibilites for variation \*/ szClassName, /\* Classname \*/ \_T("Game 1"), /\* Title Text \*/ WS\_OVERLAPPEDWINDOW | WS\_MAXIMIZE , /\* default window \*/ CW\_USEDEFAULT, /\* Windows decides the position \*/ CW\_USEDEFAULT,
Most compilers object if you put local variables inside a
case
statement. You should either declare them before the firstswitch
or enclose the case block in braces, thus:case WM\_PAINT : { PAINTSTRUCT paint ; HDC hdc = BeginPaint( hwnd , &paint ) ; RECT rect ; GetClientRect( hwnd , &rect ) ; winx = rect.right ; winy = rect.bottom ;
/*
HPEN pen = CreatePen( PS_SOLID ,
1 , RGB( 255 , 255 , 0 ) ) ;
HBRUSH brush =
CreateSolidBrush( RGB( 255 , 255 , 0 ) ) ;
*/
Ellipse( hdc ,
playerx - 20 ,
playery - 20 ,
playerx + 20 ,
playery + 20 ) ;EndPaint( hwnd , &paint ) ; } break ;
Also, in future, please provide the exact text of error messages, and indicate where they occur.
-
Most compilers object if you put local variables inside a
case
statement. You should either declare them before the firstswitch
or enclose the case block in braces, thus:case WM\_PAINT : { PAINTSTRUCT paint ; HDC hdc = BeginPaint( hwnd , &paint ) ; RECT rect ; GetClientRect( hwnd , &rect ) ; winx = rect.right ; winy = rect.bottom ;
/*
HPEN pen = CreatePen( PS_SOLID ,
1 , RGB( 255 , 255 , 0 ) ) ;
HBRUSH brush =
CreateSolidBrush( RGB( 255 , 255 , 0 ) ) ;
*/
Ellipse( hdc ,
playerx - 20 ,
playery - 20 ,
playerx + 20 ,
playery + 20 ) ;EndPaint( hwnd , &paint ) ; } break ;
Also, in future, please provide the exact text of error messages, and indicate where they occur.
update : i tryed it [ thanks for sugestions and i got somthing on screen error : my screen isn't black and isn't fulscreen my ellipse isn't yellow
// bluatigro 23 okt 2017
// win game 1#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif#include
#includebool key[ 256 ] ;
int playerx , playery , winx , winy ;
#define FRAME_TIMER 1/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);/* Make the class name into a global variable */
= _T("CodeBlocksWindowsApp");int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass *//\* The Window structure \*/ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /\* This function is called by windows \*/ wincl.style = CS\_DBLCLKS; /\* Catch double-clicks \*/ wincl.cbSize = sizeof (WNDCLASSEX); /\* Use default icon and mouse-pointer \*/ wincl.hIcon = LoadIcon (NULL, IDI\_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI\_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC\_ARROW); wincl.lpszMenuName = NULL; /\* No menu \*/ wincl.cbClsExtra = 0; /\* No extra bytes after the window class \*/ wincl.cbWndExtra = 0; /\* structure or the window instance \*/ /\* Use Windows's default colour as the background of the window \*/ wincl.hbrBackground = (HBRUSH)BLACK\_BRUSH; /\* Register the window class, and if it fails quit the program \*/ if (!RegisterClassEx (&wincl)) return 0; /\* The class is registered, let's create the program\*/ hwnd = CreateWindowEx ( 0, /\* Extended possibilites for variation \*/ szClassName, /\* Classname \*/ \_T("Game 1"), /\* Title Text \*/ WS\_OVERLAPPEDWINDOW | WS\_MAXIMIZE , /\* default window \*/ CW\_USEDEFAULT, /\* Windows decides the position \*/ CW\_USEDEFAULT, /\* where the window ends up on the screen
-
update : i tryed it [ thanks for sugestions and i got somthing on screen error : my screen isn't black and isn't fulscreen my ellipse isn't yellow
// bluatigro 23 okt 2017
// win game 1#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif#include
#includebool key[ 256 ] ;
int playerx , playery , winx , winy ;
#define FRAME_TIMER 1/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);/* Make the class name into a global variable */
= _T("CodeBlocksWindowsApp");int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass *//\* The Window structure \*/ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /\* This function is called by windows \*/ wincl.style = CS\_DBLCLKS; /\* Catch double-clicks \*/ wincl.cbSize = sizeof (WNDCLASSEX); /\* Use default icon and mouse-pointer \*/ wincl.hIcon = LoadIcon (NULL, IDI\_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI\_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC\_ARROW); wincl.lpszMenuName = NULL; /\* No menu \*/ wincl.cbClsExtra = 0; /\* No extra bytes after the window class \*/ wincl.cbWndExtra = 0; /\* structure or the window instance \*/ /\* Use Windows's default colour as the background of the window \*/ wincl.hbrBackground = (HBRUSH)BLACK\_BRUSH; /\* Register the window class, and if it fails quit the program \*/ if (!RegisterClassEx (&wincl)) return 0; /\* The class is registered, let's create the program\*/ hwnd = CreateWindowEx ( 0, /\* Extended possibilites for variation \*/ szClassName, /\* Classname \*/ \_T("Game 1"), /\* Title Text \*/ WS\_OVERLAPPEDWINDOW | WS\_MAXIMIZE , /\* default window \*/ CW\_USEDEFAULT, /\* Windows decides the position \*/ CW\_USEDEFAULT, /\* where the window ends up on the screen
You are calling
BeginPaint
for every message (not justWM_PAINT
), but you only callEndPaint
after processing aWM_PAINT
message. You need to move all code related the the painting into the block that handles theWM_PAINT
message. You also need to select your brushes and pens into the device context before you draw anything. And lastly you need to delete any DC objects that you use in painting. I would suggest studying the GDI documentation on MSDN for sample code. But be aware that GDI and GDI+ are being deprecated by Microsoft in favour of Direct2D API Reference (Windows)[^]