Chunky by FelipeFS
Chunky by FelipeFS
GPWiki.org
It is currently Sun Apr 20, 2014 1:22 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: win32 'game' example
PostPosted: Thu May 02, 2013 10:41 am 
Level 1 Cleric

Joined: Mon Aug 31, 2009 11:21 am
Posts: 10
Location: zaanstad NL
this is a exaple on how to create
a verry basic 'game' whit c++ and win32

instuctions :
- move whit cursor keys
- quit 'game' whit esc key
Code:
#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

#define FRAME_TIMER 1

HDC g_hdc ;
int g_width , g_height , g_x , g_y ;
bool key[ 255 ] ;

/*
  draw a ellipse :
  mx , my = centerpoint
  dx , dy = radius
  clrin  = color of inside
  clrout = color of line
  thick = thickness of line
*/
void ellipse(
  int mx , int my , int dx , int dy
, int clrin , int clrout , int thick )
{
   HPEN newpen = CreatePen( PS_SOLID , thick , clrout ) ;
   HBRUSH newbrush = CreateSolidBrush( clrin ) ;
   HPEN oldpen = (HPEN) SelectObject( g_hdc , newpen ) ;
   HBRUSH oldbrush = (HBRUSH) SelectObject( g_hdc , newbrush ) ;

   Ellipse( g_hdc , mx - dx / 2 , my - dy / 2
    , mx + dx / 2 , my + dy / 2 ) ;

   SelectObject( g_hdc, oldpen ) ;
   SelectObject( g_hdc, oldbrush ) ;
   DeleteObject( newpen ) ;
   DeleteObject( newbrush );
}
// primary colors
const int BLACK   = 0x000000 ;
const int RED     = 0x0000ff ;
const int GREEN   = 0x00ff00 ;
const int YELLOW  = 0x00ffff ;
const int BLUE    = 0xff0000 ;
const int MAGENTA = 0xff00ff ;
const int CYAN    = 0xffff00 ;
const int WHITE   = 0xffffff ;
//  mixed colors
const int PINK    = 0x7f7fff ;
const int ORANGE  = 0x007fff ;
const int PURPLE  = 0x7f007f ;
const int GRAY    = 0x7f7f7f ;


int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    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 */
    wincl.hbrBackground = CreateSolidBrush( BLACK ) ;

    /* 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 */
           "Windows App",       /* Title Text */
           WS_POPUP | WS_MAXIMIZE , /* fulscreen borderles */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE :
            SetTimer( hwnd
            , FRAME_TIMER
            , 40 , NULL ) ;
            g_x = g_width / 2 ;
            g_y = g_height / 2 ;
        break ;
        case WM_KEYDOWN :
            //  this is the old standerd way
            //  of exiting a program
            if ( wParam == VK_ESCAPE )
            {
                PostQuitMessage( 0 ) ;
            }
            key[ wParam ] = true ;
        break ;
        case WM_KEYUP :
            key[ wParam ] = false ;
        break ;
        case WM_TIMER :
            switch( wParam )
            {
                case FRAME_TIMER :
                    //  read cursor keys
                    if ( key[ VK_RIGHT ] )
                        g_x += 5 ;
                    if ( key[ VK_LEFT ] )
                        g_x -= 5 ;
                    if ( key[ VK_UP ] )
                        g_y -= 5 ;
                    if ( key[ VK_DOWN ] )
                        g_y += 5 ;
                    //  proses exiting screen
                    if ( g_x > g_width )
                        g_x = 0 ;
                    if ( g_x < 0 )
                        g_x = g_width ;
                    if ( g_y > g_height )
                        g_y = 0 ;
                    if ( g_y < 0 )
                        g_y = g_height ;
                    InvalidateRect( hwnd , NULL , true ) ;
            }
        break ;
        case WM_PAINT :
            PAINTSTRUCT paint ;
            g_hdc = BeginPaint( hwnd , &paint ) ;
            RECT rect ;
            GetClientRect( hwnd , &rect ) ;
            g_width = rect.right ;
            g_height = rect.bottom ;

            ellipse( g_x , g_y
            , 20 , 20 , YELLOW , RED , 5 ) ;

            EndPaint( hwnd , &paint ) ;
        break ;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

_________________
cxiu diferas el tio respondas cxiu samvaloras
[ esperanto for : everybody is diferent therefore everybody is equal ]


Top
 Profile  
 
 Post subject: Re: win32 'game' example
PostPosted: Thu May 02, 2013 1:53 pm 
Source Code Swashbuckler
User avatar

Joined: Sun Aug 05, 2012 9:32 pm
Posts: 190
I think this would be more insightful if you could rewrite it as an tutorial and add some images to show people what it looks like in the end :)

_________________
Did you ever wonder, how time works?


Top
 Profile  
 
 Post subject: Re: win32 'game' example
PostPosted: Fri May 03, 2013 11:24 am 
Bibliotherapist
User avatar

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 7004
Location: Wilts, Englandshire
It's a nice start to build on. You could add to the base to build up to a more complex game like Pac-Man or Asteroids.

How about turning it into a Wiki article with screenshots?

_________________
10 PRINT "Bad Monkey ";
20 GOTO 10


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

Powered by phpBB® Forum Software © phpBB Group