programming in c++

Off-Topic Discussions
Post Reply
madLyrical
Regular Member
Posts: 84
Joined: December 3rd, 2005, 11:28 pm
Location: north lauderdale, Fl

programming in c++

Post by madLyrical »

i havent been on here in so long coz of school.. but i wanted to see if anybody on here does programming in c++.. i got some assignment and my teacher hasnt really taught us anything, so i have like no clue where to start.. this is basically what the assignment is:

Write a windows program that neatly displays an equalateral (and equal angled) pentegon, a square, and an equilateral triangle in a window.

1) The title bar of the window will include your full name and your FAU email address.

2) When the user presses an arrow key the pentegon and only the pentegon will move slightly in the direction of the arrow.

3) When the user presses page-up the triangle will grow in size. (It will remain an equilateral triangle, and it's center will not move. )

4) When the user presses page-down the triangle will shrink in size. (It will remain an equilateral triangle, and it's center will not move. )

5) When the space bar is pressed all three graphic objects will change to a new random color

i know its simple but i just came out of C so im not familiar with the c++ set up yet.. thanks fellas :D
User avatar
shameem
Supporting Member
Posts: 820
Joined: May 9th, 2007, 9:59 pm

Re: programming in c++

Post by shameem »

This is a very simple starter exercise even though it looks formidable at first sight - There are about a million ways to do it - one way is to use OpenGL

Read up here -
http://nehe.gamedev.net/lesson.asp?index=01" onclick="window.open(this.href);return false;

Another way is to use windows GDI
http://www.functionx.com/win32/Lesson11.htm" onclick="window.open(this.href);return false;
http://www.functionx.com/visualc/gdi/polygons.htm" onclick="window.open(this.href);return false;
Image
madLyrical
Regular Member
Posts: 84
Joined: December 3rd, 2005, 11:28 pm
Location: north lauderdale, Fl

Re: programming in c++

Post by madLyrical »

that helps a little but im still having trouble trying to figuring it all out.. im trying to work off a sample code my professor gave us, but he hasnt explained enough for me to know what it all means so im just kinda guessing at it.. which isnt working out to well
ninjajim4
Regular Member
Posts: 1163
Joined: December 18th, 2004, 2:01 am

Re: programming in c++

Post by ninjajim4 »

i'd have said openGL as well but how does that have anything to do w/ c++??? seems a pretty tough first assignment to have anything graphical in a straight programming class. whatever happened to hello world..? lol
User avatar
glow
Regular Member
Posts: 43
Joined: February 9th, 2005, 9:09 pm
Location: Vancouver

Re: programming in c++

Post by glow »

lol @ hello world, i remember those days. my background was in firmware C/C++ so i can't help you when you comes to GUI stuff, sorry.

have you tried using google to see if there's any sample code/excercises that you can read and/or adapt (not copy of course) for your own benefit to help you with your assignment? sometimes i find something of use when i'm stuck on something.
94 GS
madLyrical
Regular Member
Posts: 84
Joined: December 3rd, 2005, 11:28 pm
Location: north lauderdale, Fl

Re: programming in c++

Post by madLyrical »

would u all believe on my way home from that class, the filler neck like right under the radiator cap on my gs bursts..how gay, gotta wait till thursday for the part, cost like 160 all together... but this is what i got so far, that ive figured out, yea the opengl didnt really help to much but eh... im working on trying to get the pentagon to move with a case VK_UP now



#include <windows.h>

const char g_szClassName[] = "myWindowClass";

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT Ps;
HPEN hPen;
HPEN hPen2;
HPEN hPen3;
static POINT Point[]={{300,450},{200,270},{100,450}};//triangle location
static POINT Point2[]={{600,339},{505,270},{410,339},{446,451},{564,451}};//pentagon location

switch(Msg)
{
case WM_PAINT:

hDC=BeginPaint(hWnd,&Ps);

hPen=CreatePen(PS_SOLID,8,RGB(20,20,200));
SelectObject(hDC,hPen);
Rectangle(hDC,250,25,425,200);
DeleteObject(hPen);

hPen2=CreatePen(PS_SOLID,10,RGB(200,20,20));
SelectObject(hDC,hPen2);
Polygon(hDC, Point, 3);
DeleteObject(hPen2);

hPen3=CreatePen(PS_SOLID,10,RGB(20,200,20));
SelectObject(hDC,hPen3);
Polygon(hDC, Point2, 5);
DeleteObject(hPen3);

EndPaint(hWnd,&Ps);
break;

case WM_KEYDOWN:

switch (wParam)
{
case VK_SPACE:
InvalidateRect(hWnd,NULL,true);

hDC=BeginPaint(hWnd,&Ps);

hPen=CreatePen(PS_SOLID,8,RGB(200,20,200));
SelectObject(hDC,hPen);
Rectangle(hDC,250,25,425,200);
DeleteObject(hPen);

hPen2=CreatePen(PS_SOLID,10,RGB(20,200,20));
SelectObject(hDC,hPen2);
Polygon(hDC, Point, 3);
DeleteObject(hPen2);

hPen3=CreatePen(PS_SOLID,10,RGB(200,20,20));
SelectObject(hDC,hPen3);
Polygon(hDC, Point2, 5);
DeleteObject(hPen3);

EndPaint(hWnd,&Ps);
break;
//case arrow keys:
//.......
//break;
}

break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"the title of my page" onclick="window.open(this.href);return false;",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
User avatar
shameem
Supporting Member
Posts: 820
Joined: May 9th, 2007, 9:59 pm

Re: programming in c++

Post by shameem »

you are very close to the finish line - you only need a few lines of extra code to make it work. Think of it this way - when you press a key - you are not making a box "grow" literally - you are just changing its coordinates....
Image
madLyrical
Regular Member
Posts: 84
Joined: December 3rd, 2005, 11:28 pm
Location: north lauderdale, Fl

Re: programming in c++

Post by madLyrical »

this is my updated code::
#include <windows.h>

const char g_szClassName[] = "myWindowClass";

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT Ps;
HPEN hPen;
HPEN hPen2;
HPEN hPen3;
int red=20,green=20,blue=200;
int red2=0,green2=0,blue2=0;
int red3=0,green3=0,blue3=0;
int tx1=300,tx2=200,tx3=100,ty1=450,ty2=270,ty3=450;
int px1=600,px2=505,px3=410,px4=446,px5=564,py1=339,py2=270,py3=339,py4=451,py5=451;

POINT triangle[3]={{tx1,ty1},{tx2,ty2},{tx3,ty3}};//triangle location
static POINT Point2[5]={{px1,py1},{px2,py2},{px3,py3},{px4,py4},{px5,py5}};//pentagon location

switch(Msg)
{
case WM_PAINT:

hDC=BeginPaint(hWnd,&Ps);

hPen=CreatePen(PS_SOLID,8,RGB(red,green,blue));
SelectObject(hDC,hPen);
Rectangle(hDC,250,25,425,200);
DeleteObject(hPen);

hPen2=CreatePen(PS_SOLID,10,RGB(red=red*10,green,blue=blue/10));
SelectObject(hDC,hPen2);
Polygon(hDC,triangle, 3);
DeleteObject(hPen2);

hPen3=CreatePen(PS_SOLID,10,RGB(red=red/10,green=green*10,blue));
SelectObject(hDC,hPen3);
Polygon(hDC, Point2, 5);
DeleteObject(hPen3);

EndPaint(hWnd,&Ps);
break;

case WM_KEYDOWN:
{

if(wParam == VK_SPACE)
{

InvalidateRect(hWnd,NULL,true);
hDC=BeginPaint(hWnd,&Ps);

hPen=CreatePen(PS_SOLID,8,RGB(red=rand()%255,green=rand()%256,blue=rand()%252));
SelectObject(hDC,hPen);
Rectangle(hDC,250,25,425,200);
DeleteObject(hPen);



hPen2=CreatePen(PS_SOLID,10,RGB(red2=rand()%255,green2=rand()%256,blue2=rand()%252));
SelectObject(hDC,hPen2);
Polygon(hDC, triangle, 3);
DeleteObject(hPen2);


hPen3=CreatePen(PS_SOLID,10,RGB(red3=rand()%255,green3=rand()%256,blue3=rand()%252));
SelectObject(hDC,hPen3);
Polygon(hDC, Point2, 5);
DeleteObject(hPen3);

EndPaint(hWnd,&Ps);
break;
return(red,green,blue,red2,green2,blue2,red3,green3,blue3);
}
else if(wParam == VK_PRIOR)
{
InvalidateRect(hWnd,NULL,true);
hDC=BeginPaint(hWnd,&Ps);

triangle[0].x=tx1+25;
triangle[1].y=ty2-25;
triangle[2].x=tx3-25;

hPen=CreatePen(PS_SOLID,8,RGB(red,green,blue));
SelectObject(hDC,hPen);
Rectangle(hDC,250,25,425,200);
DeleteObject(hPen);


hPen2=CreatePen(PS_SOLID,10,RGB(red2,green2,blue2));
SelectObject(hDC,hPen2);
Polygon(hDC,triangle, 3);
DeleteObject(hPen2);

hPen3=CreatePen(PS_SOLID,10,RGB(red3,green3,blue3));
SelectObject(hDC,hPen3);
Polygon(hDC, Point2, 5);
DeleteObject(hPen3);
EndPaint(hWnd,&Ps);
break;
}

/* else if(wParam == VK_NEXT)
{
InvalidateRect(hWnd,NULL,true);
hDC=BeginPaint(hWnd,&Ps);

triangle[0].x=tx1+10;
triangle[1].y=ty2+50;
triangle[2].x=tx3+50;

hPen=CreatePen(PS_SOLID,8,RGB(red,green,blue));
SelectObject(hDC,hPen);
Rectangle(hDC,250,25,425,200);
DeleteObject(hPen);


hPen2=CreatePen(PS_SOLID,10,RGB(red2,green2,blue2));
SelectObject(hDC,hPen2);
Polygon(hDC,triangle, 3);
DeleteObject(hPen2);

hPen3=CreatePen(PS_SOLID,10,RGB(red3,green3,blue3));
SelectObject(hDC,hPen3);
Polygon(hDC, Point2, 5);
DeleteObject(hPen3);
EndPaint(hWnd,&Ps);
break;
}*/

else
break;
//case arrow keys:
//.......
//break;
}

break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return triangle[0].x,triangle[1].y,triangle[2].x;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"window" onclick="window.open(this.href);return false;",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}

ive successfully got it to changed colours repeatedly but with my VK_PRIOR param, i cant get the window to remain in the changed form, so when the page up is hit again it continues to increase in size.. my screen keeps going back to the original shapes.. and does anyone know how to get the rand() function to initialize the randomated number for my colors to an int
Last edited by madLyrical on September 14th, 2008, 12:20 am, edited 1 time in total.
User avatar
shameem
Supporting Member
Posts: 820
Joined: May 9th, 2007, 9:59 pm

Re: programming in c++

Post by shameem »

WM_PAINT is the only place where you should "draw" - all other handlers should be used to change the parameters only and should not be used for drawing. Whenever Invalidate() is called the WM_PAINT will automatically be executed.
Image
Post Reply

Return to “Off-Topic”