message handling problem for game

closed account (zwA4jE8b)
I would like my MessageProc() to be similar to how it is now but the only way I know how to change members of a snake obj is to make it global. (line 19)
I want it on line 58.

How can I declare a snake object in main and allow the messageproc to update it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//Michael Ervin - Windows based graphical snake game - 7/29/2011
//////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <Windows.h>
#include "resource.h"
#include "SnakeObj.h"

#define screen_width 800
#define screen_height 600

HWND hWnd;
HDC hDC;
char _direction;
int _level, _score, _highscore;

const RECT _rect = {0,0,screen_width,screen_height};

snake _snake1(2); //Needs to not be global

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

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
	WNDCLASSEXA wc;

	ZeroMemory(&wc, sizeof(WNDCLASSEX));

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = MessageProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1)); 
	wc.lpszClassName = "SNAKE";

	RegisterClassExA(&wc);

	hWnd = CreateWindowExA(NULL,
                          "SNAKE", "Snake",
                          WS_SYSMENU,
                          0, 45,
                          screen_width, screen_height,
                          NULL, NULL,
                          hInstance,
                          NULL);

	ShowWindow(hWnd, nCmdShow);
	hDC = GetDC(hWnd);

	HBRUSH _bkbrush = CreateSolidBrush(_white);

	_level = 1;
	_score = 0;
	//snake _snake1(1);
	_snake1.init_snake(295);

	MSG msg;

	_snake1.show_head(hDC);

	while(TRUE)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;
			/*if(msg.wParam == _up || msg.wParam == _down || msg.wParam == _left || msg.wParam == _right)
				_snake1._direction = msg.wParam;*/
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
		_snake1.animate_snake(hDC);
		_snake1.next_pos();
		Sleep(75);
		}
	}
	return msg.wParam;
}

LRESULT CALLBACK MessageProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		} break;
		case WM_KEYDOWN:
		{
			switch (wParam)
			{
			case _up:
				_snake1._direction = _up;
				break;
			case _down:
				_snake1._direction = _down;
				break;
			case _left:
				_snake1._direction = _left;
				break;
			case _right:
				_snake1._direction = _right;
				break;
			}
			return 0;
		} break;
     }
     return DefWindowProcA(hWnd, message, wParam, lParam);
 }
Last edited on
closed account (zwA4jE8b)
and for those of you that want to compile and try it here is snakeobj.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//Snake object header
#ifndef snakeobj
#define snakeobj
#include <vector>

COLORREF _blue = RGB(0,0,255);
COLORREF _red = RGB(255,0,0);
COLORREF _white = RGB(255,255,255);
const int _rectwidth = 10, _rectheight = 10;
const DWORD _up = 0x57, _down = 0x53, _left = 0x41, _right = 0x44;
HBRUSH _snbrush1 = CreateSolidBrush(_blue);
HBRUSH _snbrush2 = CreateSolidBrush(_white);

class snake
{
	int _lvl, _length, _foodeaten, _counter;
	RECT _old;
	std::vector<RECT> _segments;
	
public:
	char _direction;
	snake(int _level):_lvl(_level), _length(_lvl*4), _foodeaten(0), _direction(_right), _counter(0){}

	void init_snake(int _ystart)
	{
		_segments.resize(0);
		for(int i = 0; i < _length; i++)
		{
			RECT _newseg = {395, _ystart, 395 + _rectwidth, _ystart + _rectheight};
			_segments.push_back(_newseg);
		}
	};
	void add_seg(int _x, int _y)
	{
		RECT _newseg = {_x, _y, _rectwidth, _rectheight};
		_segments.push_back(_newseg);
	}
	void show_head(const HDC& hDC)
	{
			FillRect(hDC, &_segments[0], _snbrush1); 
	}
	void next_pos()
	{
		int i;
		_old = _segments.back();

		for(i = _length - 1; i > 0; i--)
			_segments[i] = _segments[i - 1];
		
		switch (_direction)
		{
		case _right:
			_segments[0].left += _rectwidth ;
			_segments[0].right += _rectwidth;
			break;
		case _left:
			_segments[0].left -= _rectwidth;
			_segments[0].right -= _rectwidth;
			break;
		case _up:
			_segments[0].top -= _rectheight;
			_segments[0].bottom -= _rectheight;
			break;
		case _down:
			_segments[0].top += _rectheight;
			_segments[0].bottom += _rectheight;
			break;
		}
	}
	void animate_snake(const HDC& hDC)
	{
		if (_counter == ((4 * _lvl)))
			FillRect(hDC, &_old, _snbrush2);
		else
			_counter++;
		FillRect(hDC, &_segments[0], _snbrush1);
	}
	void add_seg()
	{
		RECT _newseg = _segments[_length - 1];
		_segments.push_back(_newseg);
	}
};
#endif 


also remove line 6 and 37 from the main cpp
Last edited on
Topic archived. No new replies allowed.