keypress

Pages: 12
I moved break out of the if statement but it still wont work
and I'm sorry about showering you with questions I only have a basic understanding of win 32 windows if you know of a good tutorial that would be a lot of help thanks
also
I'm 12 years old I'm not the best programmer I miss these common mistakes all of the time and this is my first time using win 32 I have pulled all of this code from many different sites there are no good win 32 tutorials
Last edited on
I moved break out of the if statement but it still wont work


It works for me. You had it working at one time, right? So maybe try backing up to where you had it working, then try adding the keydown stuff again.

I only have a basic understanding of win 32 windows if you know of a good tutorial that would be a lot of help thanks


I used to know of one, but I couldn't find it anymore. It was super old anyway so I don't know how good it would be now.

I question whether or not you really want WinAPI anyway. What kind of program are you trying to make?
I want to make graphics without a graphics library
ok I got it to run but it wont work
code:
//header files to include
#include<windows.h>
#include<stdlib.h>
#include<time.h>

//function prototypes (forward declarations)
BOOL InitInstance(HINSTANCE, int);
ATOM MyRegisterClass(HINSTANCE);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);

//the window event callback function

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
char *title = "gfx engine";
RECT rt;
int x, y, n;
COLORREF c;
int x1 = 10, y1 = 10;
if( GetAsyncKeyState( 'D' ) & 0x8000 )
{
x1 = x1 + 10;
goto redraw;
}
switch (message)
{
case WM_PAINT:
//get the dimensions of the window
redraw:
GetClientRect(hWnd, &rt);

//start drawing on devicce context
hdc = BeginPaint (hWnd, &ps);

//draw some text

c = RGB(0, 0, 0);

SetPixel(hdc, x1, 50, c);
SetPixel(hdc, x1, 51, c);

//stop drawing
EndPaint(hWnd, &ps);
break;

case WM_KEYDOWN:
if( wParam == 'D' ) // 'D' key is pressed
{
x1 ++;
InvalidateRect( hWnd, NULL, NULL ); // force the entire window to repaint
}
break;

case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}

//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//create the window class structure
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);

//fill the struct with info
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "gfx engine";
wc.hIconSm = NULL;

//set up the window with the class info
return RegisterClassEx(&wc);
}

//helper function to create the window and refresh it
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

//create a new window
hWnd = CreateWindow(
"gfx engine", //window class
"gfx engine", //title bar
WS_OVERLAPPEDWINDOW, //window style
CW_USEDEFAULT, //x position of window
CW_USEDEFAULT, //y position of window
1280, //width of the window
720, //height of the window
NULL, //parent window
NULL, //menu
hInstance, //application instance
NULL); //window parameters


//was there an error creating the window?
if(!hWnd)
return FALSE;

//display the window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//declare variables
MSG msg;

//register the class
MyRegisterClass(hInstance);

//initialize application
if(!InitInstance (hInstance, nCmdShow))
return FALSE;

//set random number seed
srand(time(NULL));

//main message loop
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
I want to make graphics without a graphics library


WinGDI (what you're using for graphics) is a graphics library. It's just a non-portable, slow, hard to use one.

It definitely has its uses, but if you're trying to make a game or something, it's the wrong tool for the job.


it wont work


You're going to have to be more specific.
Last edited on
it wont move the pixel again
I'm going to switch to the "rubber duck debug" approach.

http://en.wikipedia.org/wiki/Rubber_duck_debugging


Comment each and every line of your WinProc function, to explain what you think that line of code is doing. Then paste that here.

And when you paste it, please paste it inside of code tags:

[code]
put your code between these
[/code]
here
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//header files to include
 #include<windows.h>
 #include<stdlib.h>
 #include<time.h>

 //function prototypes (forward declarations)
 BOOL InitInstance(HINSTANCE, int);
 ATOM MyRegisterClass(HINSTANCE);
 LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);

 //the window event callback function

 LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
 PAINTSTRUCT ps; // unknown
 HDC hdc; // unknown
 char *title = "gfx engine"; //creates declares title char 
 int x1 = 10; //creates and declares x1 variable
 RECT rt;// unknown
 COLORREF c;// adds color variable
 switch (message) // begins windows message switch
 {
 case WM_PAINT: // if windows message is to redraw
 //get the dimensions of the window
redraw://                         |
 GetClientRect(hWnd, &rt);//      |
 //--------------------------------


 hdc = BeginPaint (hWnd, &ps); //start drawing on device context

 c = RGB(0, 0, 0);//declares color of pixels

 SetPixel(hdc, x1, 50, c);// adds pixel
 SetPixel(hdc, x1, 51, c);// adds pixel

 
 EndPaint(hWnd, &ps);//stop drawing
 break; //breaks case

 case WM_KEYDOWN: // if windows message is a key down
    if( wParam == 'D' )  // if D key is pressed
    {
        x1 ++; // ads one to x1 therefore moving the pixels upon redraw
        InvalidateRect( hWnd, NULL, NULL );  // force the entire window to repaint
    }
    break;// breaks case

 case WM_DESTROY:// if windows message is to close
 PostQuitMessage(0);//close
 break;// breaks case
 }
 return DefWindowProc(hWnd, message, wParam, lParam);// unknown
 }

 //helper function to set up the window properties
 ATOM MyRegisterClass(HINSTANCE hInstance)
 {
 //create the window class structure
 WNDCLASSEX wc;
 wc.cbSize = sizeof(WNDCLASSEX);

 //fill the struct with info
 wc.style = CS_HREDRAW | CS_VREDRAW;
 wc.lpfnWndProc = (WNDPROC)WinProc;
 wc.cbClsExtra = 0;
 wc.cbWndExtra = 0;
 wc.hInstance = hInstance;
 wc.hIcon = NULL;
 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
 wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
 wc.lpszMenuName = NULL;
 wc.lpszClassName = "gfx engine";
 wc.hIconSm = NULL;

 //set up the window with the class info
 return RegisterClassEx(&wc);
 }

 //helper function to create the window and refresh it
 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
 {
 HWND hWnd;

 //create a new window
 hWnd = CreateWindow(
 "gfx engine", //window class
 "gfx engine", //title bar
 WS_OVERLAPPEDWINDOW, //window style
 CW_USEDEFAULT, //x position of window
 CW_USEDEFAULT, //y position of window
 1280, //width of the window
 720, //height of the window
 NULL, //parent window
 NULL, //menu
 hInstance, //application instance
 NULL); //window parameters


 //was there an error creating the window?
 if(!hWnd)
 return FALSE;

 //display the window
 ShowWindow(hWnd, nCmdShow);
 UpdateWindow(hWnd);

 return TRUE;
 }

 //entry point for a Windows program
 int WINAPI WinMain(HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPSTR lpCmdLine,
 int nCmdShow)
 {
 //declare variables
 MSG msg;

 //register the class
 MyRegisterClass(hInstance);

 //initialize application
 if(!InitInstance (hInstance, nCmdShow))
 return FALSE;

 //set random number seed
 srand(time(NULL));

 //main message loop
 while(GetMessage(&msg, NULL, 0, 0))
 {
 TranslateMessage(&msg);
 DispatchMessage(&msg);
 }
 return msg.wParam;
 }

Remember that WinProc is called separately for each message you process. One keypress and one redraw are 2 separate messages and therefore 2 separate function calls.

With that in mind... what would you expect this program to output?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

void func()
{
    int var = 10;

    cout << var;

    ++var;
}

int main()
{
    // call func 3 times:
    func();
    func();
    func();
}



Then look at line 18 of your program.


(hint: I've already told you about this problem previously in this thread, you just never fixed it)
Last edited on
16???
oh sorry never mind now I get what you meant and also here's why I'm doing this (I set this up so you can be an admin if you want): http://communitygame.wikia.com/wiki/Communitygame_Wiki

if you want to also help with it that would be great
Communitygame_Wiki

You're intending to share code via a wiki?

Why not use the more usual approach of a shared code repository, like GitHub, BitBucket, SourceForge, Google Code, CodePlex, ... ??

Andy
Last edited on
well I would like to but have no Idea how to do that plus I'd like the project to start to grow before it gets advanced
Topic archived. No new replies allowed.
Pages: 12