C++ ONLY// A allegro 5 program in c++ only that stays in a loop until escape is
ID: 3851401 • Letter: C
Question
C++ ONLY//
A allegro 5 program in c++ only that stays in a loop until escape is pressed. It shows a background image and moves an object on screen using four arrow keys but the object can only move if its center stays on white background, see the ghost in the following screen shot., create a background bitmap that is black and has a white path, Use a small player character, a function that reads a pixel colour, A colour is white if RGB components are 255. No actual BMP needed just code, will insert bmp where it is needed
Explanation / Answer
GetAsyncKeyStat() is the function to be used to check whether the key is pressed or not, for this to execute you will have to include <Windows.h> which gives you excess to GetAsyncKeyStat() function and to the windows marcos. Next you will have to initialise a constant in your code to read the most significant bit to evaluate whether the is being pressed or not (MSB = 0x8000).
const unsigned short MSB = 0x8000;
bool listenKeyPress(short p_key)
{
if (GetAsyncKeyState(p_key) & MSB)
{
return true;
}
else return false;
}
For the background image it can be done using CPictureWindow Class, which uses one of the ATL classes, CWindowImpl, Therefore type these two lines in your stdafx.h
#include < atlbase.h > ;
extern CComModule _Module;
now you will have to add a new member of CPictureWindow in the object after adding subclass the object with your new member. After this you will have call load function of CPictureWindow Class which will display your background image.
#include "PictureWindow.h"
{
...
}
To create an movable objects initalise the two variables i and j to center coordinates, now intialise another two variables x and y to monitor the movement of the object. To track the key press use the function GetAsyncKeyStat or kbhit(). Use a variable to read the key press status, use the ASCII values of the arrow keys to mark the key press status ( For example : ASCII value for right arrow key is 77 ) and put this onto a loop.
The default background color for the console is usually black, you can keep it as black and go on or change it to the desired color referring to the RGB component pallet.
The other option is using opengl functions
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
}
For opengl to work you have to include :
#include <GL/glut.h>
For the reaction of the white path you have to play around with the coordinates, you can create the path by varing the different coordinates at different part of your console, this could take an extra effort. Further to get the pixel value you can use a function GetPixel().
Things can get better if you go with opengl for creating of white path. it is working with vertices, these are the points from which shapes like triangles, rectangles will later be constructed. Each of these points is stored with certain attributes and it's up to you to decide what kind of attributes you want to store.
A Sample code for creating of triangle using opengl
glBegin(GL_TRIANGLES); // Each set of 3 vertices form a triangle
glVertex2f(0.1f, -0.6f);
glVertex2f(0.7f, -0.6f);
glVertex2f(0.4f, -0.1f);
A Sample code for creating of polygon using opengl
glBegin(GL_POLYGON); // These vertices form a closed polygon
glVertex2f(0.4f, 0.2f);
glVertex2f(0.6f, 0.2f);
glVertex2f(0.7f, 0.4f);
glVertex2f(0.6f, 0.6f);
glVertex2f(0.4f, 0.6f);
glVertex2f(0.3f, 0.4f);
glEnd();
The white path can be created as a polygon or a sequence of polygons.
In the main() function with opengl can be as follows:
main ()
{
glutInit(&argc, argv);
glutCreateWindow("Vertex, Primitive & Color"); create a window a title
glutInitWindowSize(320, 320); // specify the console window size
glutInitWindowPosition(50, 50); // specify the console window position
....
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.