Related
I'm using floodfill() and it's not coloring the places I want it to, instead it colors the entire window.
I want the Cyan background inside the rectangle and the magenta under the line line(conx(30) - 2, cony(0)+2, conx(100) + 2, cony(30) - 2); but still within the rectangle boundary.
Here's the code, with relevant libraries included:
#include <iostream>
#include <graphics.h>
#include <cmath>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
using namespace std;
//convert to pixel value (scale of 6)
double conx(double x)
{
return x * (600/100) + 50;
}
double cony(double y)
{
return -y * (600/100) + 650;
}
int main()
{
initwindow(700,700);
rectangle(50, 0, 650, 650);
setfillstyle(SOLID_FILL, CYAN);
floodfill(100, 100, CYAN);
setfillstyle(SOLID_FILL, MAGENTA);
floodfill(620, 620, MAGENTA);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 3);
outtextxy(150, 655, "ELASTIC PARTICLE");
setcolor(15);
setcolor(15);
line(0, 0, 700, 0);
line(50, 0, 50, 650);
line(650, 0, 650, 652);
line(50, 650, 652, 650);
//drawing the line for the wedge/incline
line(conx(30) - 2, cony(0)+2, conx(100) + 2, cony(30) - 2);
//borders
setcolor(15);
line(0, 0, 700, 0);
line(50, 0, 50, 650);
line(650, 0, 650, 652);
line(50, 650, 652, 650);
}
Here's the sample of the output
My code works correctly but when I resize the output window, ground on the sceen is distorted. How can I fix it?Correct output is below.When I resize the output window, it becomes like the below images.
Correct output is below.
My code is below:
#define _USE_MATH_DEFINES
#include <cmath>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
int x = 0;
int z = 0;
int y;
int data[17][21] =
{ { 14 ,25, 45 ,55 ,68 ,70 ,84 ,91 ,97, 101 ,105 ,105 ,105, 105 ,110 ,110, 110, 110 ,110, 110, 110 },
{ 5, 18, 43, 62 ,73, 82, 88, 94, 99, 102 ,105, 105 ,105, 105, 110, 110 ,110 ,110 ,110, 110, 110 },
{ 5, 18 ,38 ,56, 69, 77, 86, 94, 99, 103, 106, 105, 105, 105, 110, 110, 110, 110, 110, 110, 110 },
{ 5 ,9 ,31, 48, 60, 71, 81, 87, 95, 101, 106, 105, 105, 105, 110, 110, 110, 110, 110, 110, 110 },
{ 5, 5, 18, 37, 49, 56, 62, 81, 91, 94, 101, 105, 105, 105, 110, 110, 110 ,110 ,110, 110, 110 },
{ 5, 5, 12, 23 ,34, 40, 53 ,66 ,77 ,82, 97, 103, 105, 105, 109, 110, 110, 110, 110, 115, 115 },
{ 4 ,5 ,8 ,15, 20, 24, 35, 39, 40, 77, 92, 101, 104, 104 ,105, 110, 110, 110, 115, 115, 115 },
{ 5, 7 ,22, 36, 46, 48, 48, 44 ,50, 58, 80, 96, 96, 97, 106, 110, 110, 115, 115, 115, 115 },
{ 4, 15 ,31 ,46 ,61, 68, 69, 63, 53, 50, 67, 82, 84, 103, 108, 110, 110, 115, 115, 115, 115 },
{ 4, 12, 31, 46, 64, 78, 82, 80, 69, 54, 73, 71, 92, 105, 108, 110, 110, 115, 115, 115, 115 },
{ 6, 26 ,35 ,45, 63, 75, 84, 87, 84, 74 ,77, 80, 96, 103, 108, 110, 110, 110, 115, 115, 115 },
{ 21, 30, 46, 57 ,64 ,76 ,85 ,92 ,92, 87 ,79 ,80 ,86 ,102, 106, 110, 105 ,110, 115, 115, 115 },
{ 27, 40, 48 ,62 ,75 ,84 ,92, 96, 97 ,94 ,88 ,80 ,80 ,91, 104, 105, 105, 105, 110, 115, 115 },
{ 33, 43, 55, 65, 75, 87, 96, 101, 101, 101, 97, 92, 80, 80, 98, 105, 105, 105, 105, 110, 115 },
{ 45, 50, 58, 68, 80, 91, 99, 102, 105, 105, 105, 99, 90, 80, 80, 97, 105, 105, 105, 110, 100 },
{ 50, 60, 65, 71, 84, 95, 101, 105, 105, 107, 107, 106, 102, 101, 92, 80, 98, 104, 105, 100, 100 },
{ 60, 70, 76, 83, 88 ,96, 103, 106 ,107, 108 ,110, 109 ,108 ,108, 106, 101 ,90, 100, 100, 100, 100 } };
//bool tamam=true;
void display(void)
{
int type = GL_TRIANGLES; // or GL_LINE_LOOP
glLoadIdentity();
gluLookAt(350, 600, 400, 280, 300, 300, 0, 0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Sides of the cube as loops or polygons, in anti-clockwise order.
//glColor3f(1.0, 1.0, 1.0);
glBegin(type);
for (int i = 0; i < 16; i++)
{
x = 0;
for (int k = 0; k < 20; k++)
{
y = data[i][k];
if (y >= 80)
glColor3f(1.0, 0.0, 0.0);
if (y>0 && y<50)
glColor3f(0.0, 1.0, 0.0);
if (y >= 50 && y<80)
glColor3f(1.0, 1.0, 0.0);
glVertex3f(x, data[i][k], z);
glVertex3f(x, data[i + 1][k], z + 20);
glVertex3f(x + 20, data[i + 1][k + 1], z + 20);
x = x + 20;
}
z = z + 20;
}
z = 0;
for (int i = 0; i < 16; i++)
{
x = 0;
for (int k = 0; k < 20; k++)
{
y = data[i][k];
if (y>0 && y<50)
glColor3f(0.0, 1.0, 0.0);
if (y >= 50 && y<80)
glColor3f(1.0, 1.0, 0.0);
if (y >= 80)
glColor3f(1.0, 0.0, 0.0);
glVertex3f(x + 20, data[i + 1][k + 1], z + 20);//z*i
glVertex3f(x + 20, data[i][k + 1], z);
glVertex3f(x, data[i][k], z);
x = x + 20;
}
z = z + 20;
}
glEnd(); // front
/*if(tamam)*/
/*tamam=false;*/
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: case 'q': case 'Q':
exit(EXIT_SUCCESS);
break;
}
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1200, 720);
glutCreateWindow(argv[0]);
glViewport(0, 0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(300, glutGet(GLUT_WINDOW_WIDTH) /
glutGet(GLUT_WINDOW_HEIGHT), 0.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
//glutReshapeFunc(reshape);
//glEnable(GL_DEPTH_TEST);
glutMainLoop();
return EXIT_SUCCESS;
}
How can I fix it??
Thanks for helping...
This looks like an indexing error. Why are x, y, z (and probably also i) global variables? Most likely you're left one of them in a bad state, and the next time you iterate over your data it causes this mess.
BTW: You should strongly consider to ditch that fixed function pipeline immediate mode drawing. Place your data array into a VBO, build an index array that will iterate over it forming triangles (like you currently do) and use the vertex shader variable gl_VertexID in addition with a width specifying uniform to produce the x and z positions for the vertex.
I wonder whether it is because of the use of integer division in the call to gluPerspective. Kind of a guess.
This code works with no errors but the ground that I created is not seen on the screen after running the program. I want to see the whole ground on the screen.
How can I fix the code?
#define A glVertex3f (0.5, 1.2, 0)
#define B glVertex3f (1, 2, 0)
#define C glVertex3f (2, 1.4, 0)
#define D glVertex3f ( 0.5, 0.5, -0.5)
#define E glVertex3f (-0.5, 0.5, 0.5)
#define F glVertex3f (-0.5, -0.5, 0.5)
#define G glVertex3f ( 0.5, -0.5, 0.5)
#define H glVertex3f ( 0.5, 0.5, 0.5)
#define _USE_MATH_DEFINES
#include <cmath>
#include <stdlib.h>
#include <math.h>
#include <glut.h>
float distance = 5.0;
int longitude = 0, latitude = 0, ainc = 5;
int lastx = -1, lasty = -1;
int x=0,z=0;
int y;
int data[17][21]=
{{14 ,25, 45 ,55 ,68 ,70 ,84 ,91 ,97, 101 ,105 ,105 ,105, 105 ,110 ,110, 110, 110 ,110, 110, 110},
{5, 18, 43, 62 ,73, 82, 88, 94, 99, 102 ,105, 105 ,105, 105, 110, 110 ,110 ,110 ,110, 110, 110},
{5, 18 ,38 ,56, 69, 77, 86, 94, 99, 103, 106, 105, 105, 105, 110, 110, 110, 110, 110, 110, 110},
{5 ,9 ,31, 48, 60, 71, 81, 87, 95, 101, 106, 105, 105, 105, 110, 110, 110, 110, 110, 110, 110},
{5, 5, 18, 37, 49, 56, 62, 81, 91, 94, 101, 105, 105, 105, 110, 110, 110 ,110 ,110, 110, 110},
{5, 5, 12, 23 ,34, 40, 53 ,66 ,77 ,82, 97, 103, 105, 105, 109, 110, 110, 110, 110, 115, 115},
{4 ,5 ,8 ,15, 20, 24, 35, 39, 40, 77, 92, 101, 104, 104 ,105, 110, 110, 110, 115, 115, 115},
{5, 7 ,22, 36, 46, 48, 48, 44 ,50, 58, 80, 96, 96, 97, 106, 110, 110, 115, 115, 115, 115},
{4, 15 ,31 ,46 ,61, 68, 69, 63, 53, 50, 67, 82, 84, 103, 108, 110, 110, 115, 115, 115, 115},
{4, 12, 31, 46, 64, 78, 82, 80, 69, 54, 73, 71, 92, 105, 108, 110, 110, 115, 115, 115, 115},
{6, 26 ,35 ,45, 63, 75, 84, 87, 84, 74 ,77, 80, 96, 103, 108, 110, 110, 110, 115, 115, 115},
{21, 30, 46, 57 ,64 ,76 ,85 ,92 ,92, 87 ,79 ,80 ,86 ,102, 106, 110, 105 ,110, 115, 115, 115},
{27, 40, 48 ,62 ,75 ,84 ,92, 96, 97 ,94 ,88 ,80 ,80 ,91, 104, 105, 105, 105, 110, 115, 115},
{33, 43, 55, 65, 75, 87, 96, 101, 101, 101, 97, 92, 80, 80, 98, 105, 105, 105, 105, 110, 115},
{45, 50, 58, 68, 80, 91, 99, 102, 105, 105, 105, 99, 90, 80, 80, 97, 105, 105, 105, 110, 100},
{50, 60, 65, 71, 84, 95, 101, 105, 105, 107, 107, 106, 102, 101, 92, 80, 98, 104, 105, 100, 100},
{60, 70, 76, 83, 88 ,96, 103, 106 ,107, 108 ,110, 109 ,108 ,108, 106, 101 ,90, 100, 100, 100, 100}};
void display (void)
{
float xc, yc, zc;
int type = GL_TRIANGLE_STRIP; // or GL_LINE_LOOP
xc = distance * cos (latitude /180.0*M_PI) * cos (longitude/180.0*M_PI);
yc = distance * sin (latitude /180.0*M_PI);
zc = distance * cos (latitude /180.0*M_PI) * sin (longitude/180.0*M_PI);
glLoadIdentity ();
gluLookAt (xc, yc, zc, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glBegin (type);
for (int i = 0; i < 17; i++)
{
z=z+20;
for (int k = 0; k < 21; k=k+1)
{
y=data[i][k];
if(y>0&&y<50)
glColor3f(0.0, 1.0, 0.0);
if(y>=50&&y<80)
glColor3f(1.0, 1.0, 0.0);
if(y>=80)
glColor3f(1.0, 0.0, 0.0);
glVertex3f(x, data[i][k], z);
x=x+20;
}
}
glEnd(); // front
glutSwapBuffers ();
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 27: case 'q': case 'Q':
exit (EXIT_SUCCESS);
break;
}
}
void special (int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
distance *= 2;
break;
case GLUT_KEY_DOWN:
distance /= 2;
break;
}
glutPostRedisplay ();
}
void click (int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
lastx = x;
lasty = y;
}
}
void mouse (int x, int y)
{
if (x > lastx) {
longitude = (longitude + ainc) % 360;
} else if (x < lastx) {
longitude = (longitude - ainc) % 360;
}
if (y > lasty) {
latitude = (latitude + ainc) % 360;
} else if (y < lasty) {
latitude = (latitude - ainc) % 360;
}
lastx = x;
lasty = y;
glutPostRedisplay ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (65.0, (GLfloat) w / (GLfloat) h, 1.0, 20.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.0, 0.0, -5.0);
}
int main (int argc, char *argv[])
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow (argv[0]);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(50.0, 1.0, 3.0, 7.0);
glMatrixMode (GL_MODELVIEW);
glutDisplayFunc (display);
glutKeyboardFunc (keyboard);
glutSpecialFunc (special);
glutMouseFunc (click);
glutMotionFunc (mouse);
glutReshapeFunc (reshape);
glEnable (GL_DEPTH_TEST);
glutMainLoop ();
return EXIT_SUCCESS;
}
I can see few things which you can check
1) in reshape Fucntion glTranslatef (0.0, 0.0, -5.0) is called, just after that in display function you are calling loadIdentity() which will reset the matrix. So translate may not apply on the object you are drawing.
2) Also try changing the Z value more inwards Z axis.
3) Change your projection Far point, and FOV angle, as your X values are very higher.
I noticed one more thing your x,y,z are global values and are not reset on reshape.
Hope it helps.
I have a function in which a bunch of stuff is drawn into an offscreen buffer. At the end of the function, it calls InvalidateRect. For some reason, sometimes it redraws halfway through the function, causing a flicker. Here's the code for the function:
// Side Info
HBITMAP side = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_SIDEINFO));
hdcold = (HBITMAP)SelectObject(hbcmem, side);
BitBlt(hdcmem, 339, 26, 154, 300, hbcmem, 0, 0, SRCCOPY);
DrawLevelNumber(game.levelnumber);
if (color)
sprites = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_COLOR_SPRITES));
else sprites = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BLACKWHITE_SPRITES));
hdcold = (HBITMAP)SelectObject(hbcmem, sprites);
// Find x and y coordinate for the top left of the visible screen
int x = game.Player_x, y = game.Player_y, ypos = 0;
if (x < 4) x = 4;
if (x > 27) x = 27;
if (y < 4) y = 4;
if (y > 27) y = 27;
x -= 4;
y -= 4;
// Draw lower layer
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (game.Layer_Two[x + i][y + j] != 0)
{
int xpos = game.get_pos(game.Layer_Two[x + i][y + j], ypos, false);
BitBlt(hdcmem, (i * 32) + 32, (j * 32) + 32, 32, 32, hbcmem, xpos, ypos, SRCCOPY);
}
}
}
// Draw upper layer
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if ((game.Layer_Two[x + i][y + j] != 0 && game.Layer_One[x + i][y + j] >= 64 && game.Layer_One[x + i][y + j] <= 111))
{
int xpos = game.get_pos(game.Layer_One[x + i][y + j], ypos, true);
BitBlt(hdcmem, (i * 32) + 32, (j * 32) + 32, 32, 32, hbcmem, xpos + 96, ypos, SRCPAINT);
BitBlt(hdcmem, (i * 32) + 32, (j * 32) + 32, 32, 32, hbcmem, xpos, ypos, SRCAND);
} else {
int xpos = game.get_pos(game.Layer_One[x + i][y + j], ypos, false);
BitBlt(hdcmem, (i * 32) + 32, (j * 32) + 32, 32, 32, hbcmem, xpos, ypos, SRCCOPY);
}
}
}
// If it isn't started, show title
if (!game.started)
{
HDC tmphdc = CreateCompatibleDC(hdcmem);
HDC tmp = CreateCompatibleDC(tmphdc);
RECT rc;
GetClientRect(hWnd, &rc);
string str = game.leveltitle.substr(0, game.leveltitle.length() - 1) + "\nPassword: " + game.password;
TCHAR* tch = new TCHAR[str.length()];
mbstowcs_s(NULL, tch, _tcslen(tch), str.c_str(), str.length());
HFONT font = CreateFont(25, 0, 0, 0, FW_BOLD, false, false, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, NULL);
SelectObject(tmp, font);
DrawText(tmp, tch, str.length(), &rc, DT_CALCRECT);
rc.right += 16;
HBITMAP tmpbm = CreateCompatibleBitmap(hdcmem, rc.right, rc.bottom);
HBITMAP tmpold = (HBITMAP)SelectObject(tmphdc, tmpbm);
HBRUSH hbr = CreateSolidBrush(RGB(255, 255, 255));
HPEN pen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
SelectObject(hdcmem, pen);
SelectObject(hdcmem, hbr);
Rectangle(hdcmem, 176 - (rc.right / 2), 243, 177 + (rc.right / 2), 248);
hbr = CreateSolidBrush(RGB(128, 128, 128));
pen = CreatePen(PS_SOLID, 1, RGB(128, 128, 128));
SelectObject(hdcmem, pen);
SelectObject(hdcmem, hbr);
Rectangle(hdcmem, 176 - (rc.right / 2), 294, 177 + (rc.right / 2), 299);
HBITMAP left = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_LEFT));
hdcold = (HBITMAP)SelectObject(hbcmem, left);
BitBlt(hdcmem, 176 - (rc.right / 2) - 4, 243, 4, 56, hbcmem, 0, 0, SRCCOPY);
HBITMAP right = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_RIGHT));
hdcold = (HBITMAP)SelectObject(hbcmem, right);
BitBlt(hdcmem, 176 + (rc.right / 2) + 1, 243, 4, 56, hbcmem, 0, 0, SRCCOPY);
SelectObject(tmphdc, font);
SetTextColor(tmphdc, RGB(255, 255, 0));
SetBkColor(tmphdc, RGB(0, 0, 0));
DrawText(tmphdc, tch, str.length(), &rc, DT_CENTER);
BITMAP structBitmapHeader;
memset( &structBitmapHeader, 0, sizeof(BITMAP) );
HGDIOBJ hBitmap = GetCurrentObject(tmphdc, OBJ_BITMAP);
GetObject(hBitmap, sizeof(BITMAP), &structBitmapHeader);
BitBlt(hdcmem, 176 - (rc.right / 2), 247, structBitmapHeader.bmWidth, structBitmapHeader.bmHeight, tmphdc, 0, 0, SRCCOPY);
}
// If paused
if (game.paused)
{
RECT rc;
rc.top = 32;
rc.left = 32;
rc.bottom = 330;
rc.right = 330;
BitBlt(hdcmem, 32, 32, 288, 288, NULL, 0, 0, BLACKNESS);
HFONT font = CreateFont(50, 0, 0, 0, FW_NORMAL, false, false, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, NULL);
SelectObject(hdcmem, font);
SetTextColor(hdcmem, RGB(255, 0, 0));
SetBkColor(hdcmem, RGB(0, 0, 0));
DrawText(hdcmem, L"PAUSED", 6, &rc, (DT_CENTER + DT_SINGLELINE + DT_VCENTER));
}
nums = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_NUMBERS));
hdcold = (HBITMAP)SelectObject(hbcmem, nums);
for (int i = 100; i > 0; i /= 10) // Coins left
{
int tmp;
if (i == 100)
tmp = game.coinsleft / 100;
if (i == 10)
tmp = ((game.coinsleft % 100) - (game.coinsleft % 10)) / 10;
if (i == 1)
tmp = game.coinsleft % 10;
if (game.coinsleft < i && i > 1)
tmp = 10;
int ypos = game.get_num_pos(tmp, (game.coinsleft == 0));
BitBlt(hdcmem, 369 + ((3 - log10((double)i)) * 17), 215, 17, 23, hbcmem, 0, ypos, SRCCOPY);
if (i == 100)
tmp = game.timeleft / 100;
if (i == 10)
tmp = ((game.timeleft % 100) - (game.timeleft % 10)) / 10;
if (i == 1)
tmp = game.timeleft % 10;
if (game.timeleft < i && i > 1)
tmp = 10;
if (game.timelimit == 0)
tmp = 11;
ypos = game.get_num_pos(tmp, (game.timeleft < 16 || game.timelimit == 0));
BitBlt(hdcmem, 369 + ((3 - log10((double)i)) * 17), 125, 17, 23, hbcmem, 0, ypos, SRCCOPY);
}
if (game.onhint)
{
HBITMAP sidebg = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_SIDEBG));
hdcold = (HBITMAP)SelectObject(hbcmem, sidebg);
BitBlt(hdcmem, 353, 165, 127, 146, hbcmem, 0, 0, SRCCOPY);
} else {
hdcold = (HBITMAP)SelectObject(hbcmem, sprites); // LOWER SIDE INFO
if (game.key1 > 0)
BitBlt(hdcmem, 352, 247, 32, 32, hbcmem, 192, 160, SRCCOPY);
else BitBlt(hdcmem, 352, 247, 32, 32, hbcmem, 0, 0, SRCCOPY);
if (game.key2 > 0)
BitBlt(hdcmem, 384, 247, 32, 32, hbcmem, 192, 128, SRCCOPY);
else BitBlt(hdcmem, 384, 247, 32, 32, hbcmem, 0, 0, SRCCOPY);
if (game.key3 > 0)
BitBlt(hdcmem, 416, 247, 32, 32, hbcmem, 192, 224, SRCCOPY);
else BitBlt(hdcmem, 416, 247, 32, 32, hbcmem, 0, 0, SRCCOPY);
if (game.key4)
BitBlt(hdcmem, 448, 247, 32, 32, hbcmem, 192, 192, SRCCOPY);
else BitBlt(hdcmem, 448, 247, 32, 32, hbcmem, 0, 0, SRCCOPY);
if (game.water)
BitBlt(hdcmem, 352, 279, 32, 32, hbcmem, 192, 256, SRCCOPY);
else BitBlt(hdcmem, 352, 279, 32, 32, hbcmem, 0, 0, SRCCOPY);
if (game.fire)
BitBlt(hdcmem, 384, 279, 32, 32, hbcmem, 192, 288, SRCCOPY);
else BitBlt(hdcmem, 384, 279, 32, 32, hbcmem, 0, 0, SRCCOPY);
if (game.ice)
BitBlt(hdcmem, 416, 279, 32, 32, hbcmem, 192, 320, SRCCOPY);
else BitBlt(hdcmem, 416, 279, 32, 32, hbcmem, 0, 0, SRCCOPY);
if (game.suction)
BitBlt(hdcmem, 448, 279, 32, 32, hbcmem, 192, 352, SRCCOPY);
else BitBlt(hdcmem, 448, 279, 32, 32, hbcmem, 0, 0, SRCCOPY);
}
RECT rc;
rc.left = 0;
rc.top = 0;
rc.right = 518;
rc.bottom = 401;
InvalidateRect(hWnd, &rc, false);
What is causing the flicker?
Ensure that you handle the WM_ERASEBKGND message and return TRUE, this will prevent the window base class from drawing the background.
Instead of calling InvalidateRect at the end of your drawing routine, place your drawing code inside a WM_PAINT message handler, this ensures your drawing code is called whenever the window is being repainted.
I'm trying to make an easy way to print X's and O's to the grid below without using multiple If-Else statements. I'm using a screen size of 575x620 for the application.
Rectangle(hdc, 5, 570, 550, 5);
Rectangle(hdc, 50, 50, 200, 200);
Rectangle(hdc, 200, 50, 350, 200);
Rectangle(hdc, 350, 50, 500, 200);
Rectangle(hdc, 50, 200, 200, 350);
Rectangle(hdc, 200, 200, 350, 350);
Rectangle(hdc, 350, 200, 500, 350);
Rectangle(hdc, 50, 350, 200, 500);
Rectangle(hdc, 200, 350, 350, 500);
Rectangle(hdc, 350, 350, 500, 500);
if( ttt.board[ 0 ][ 0 ] == 1 )
{
MoveToEx(hdc, 50, 50, NULL);
LineTo(hdc, 200, 200);
MoveToEx(hdc, 200, 50, NULL);
LineTo(hdc, 50, 200);
}
else
{
Ellipse(hdc, 50, 50, 200, 200);
}
The code above is printing the first X and O to the first block and I would like to do the same for the remaining blocks without a ton of If-Else statements. Anyone have an idea or suggestions on how I can implement this? The data member is checking wither or not I clicked the box.
I would write a draw_cross function, to which you pass one of the corners (or the center, if you prefer) of the square where it should draw. Likewise, a draw_circle. Then step through the board, and use those to draw the appropriate one in each square.