Hi all,

I'm just working on a little Allegro 5 puzzle game for a bit of fun and practise between jobs.

Anyway, I have a bit of an optimization issue. It's a board based game where you have an 8x8 board with each square having a value of 0-3 representing what is on the square, this info is saved in an array.

So I want the squares to be highlighted when the mouse is positioned over them. I've done this by adding a function that whenever the mouse is moved, checks to see it it is now over a different square, and if it is, draws a bitmap over the square which highlights it. However, the function currently redraws the entire board every time it draws the highlight sprite since the screen goes black, just drawing the highlighter sprite if I don't call my redraw function.

Can anyone suggest a way that I can either just redraw the square the mouse is over rather than the whole board or store the board (made up of 64 bitmaps drawn to the screen) as a new bitmap and just draw that to the screen as 1 bitmap each time the funciton is called rather than each of the 64 bitmaps individually?

Here is my code for this function:

if(ev.type == ALLEGRO_EVENT_MOUSE_AXES && ev.mouse.x < 500 && ev.mouse.y < 500)
		{
			mousex = ev.mouse.x/50;
			mousey = ev.mouse.y/50;
			currmousepos[0] = mousey*50;
			currmousepos[1] = mousex*50;
			if(currmousepos[0] != lastmousepos[0] || currmousepos[1] != lastmousepos[1])
			{

			printscore();
			redraw();
			
			al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);

			al_draw_bitmap(highlight,mousex*50,mousey*50,0);
			al_draw_bitmap(pass,500,300,0);
			
			
			al_flip_display();
			}
			lastmousepos[0] = currmousepos[0];
			lastmousepos[1] = currmousepos[1];
		}

Dani AI

Generated

Quick diagnosis and practical fixes for 's highlight flicker:

printscore() clears the backbuffer (al_clear_to_color). Because the backbuffer is cleared, the code must fully redraw the board before drawing the highlight — which is why the screen goes black unless redraw() is called. Two simple, robust approaches remove that cost.

Preferred: render-to-texture once, blit every frame

  • Create an offscreen bitmap the size of the board, render the 10x10 tiles into it once (or only when tiles change), then in the mouse-move handler just blit that single bitmap and draw the highlight on top. Update only changed tiles back into the offscreen buffer when a move happens. Example skeleton:
ALLEGRO_BITMAP *board_buf = al_create_bitmap(500, 500);
ALLEGRO_BITMAP *prev_target = al_get_target_bitmap();
al_set_target_bitmap(board_buf);
for (y = 0; y < rows; ++y)
  for (x = 0; x < cols; ++x)
    al_draw_bitmap(tile_for(map[y][x]), x*TILE, y*TILE, 0);
al_set_target_bitmap(prev_target);

/* on mouse move: */
al_draw_bitmap(board_buf, 0, 0, 0);
al_draw_bitmap(highlight, mousex*TILE, mousey*TILE, 0);
al_draw_bitmap(pass, 500, 300, 0);
al_flip_display();

Lighter-weight: redraw only the two tiles (previous and current)

  • Instead of full redraw(), re-draw the single tile under the old cursor position (to remove old highlight) and the current tile, then draw the highlight over the new tile. Use al_set_clipping_rectangle to limit redraw to the small region if desired.

Other concrete tips (from reading your posted code)

  • Remove al_clear_to_color from printscore(); have it only draw text (or draw score into its own small bitmap) so it won’t wipe the board.
  • Use al_hold_bitmap_drawing(true) around bulk draws to speed up drawing many bitmaps.
  • Create the event queue and register event sources once at init — don’t allocate one inside the main loop.
  • Free temporary bitmaps with al_destroy_bitmap when no longer needed and update board_buf only when the map actually changes.

These changes will eliminate the black flash and let the highlight be drawn with a single, cheap blit instead of re-drawing all 64 tiles each mouse move.

Can you post your full code please?

Can you post your full code please?

Yes. Here it is :P

It's quite lengthy and completely linear, need to get round to making it more modular and using classes etc. When I started programming it I got lazy lol

// Turnover.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include "turnoverengine.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro5.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <iostream>
#include <fstream>
#include <mmsystem.h>
#include <process.h>
#include "math.h"
#include "string.h"
#include <sstream>


using namespace std;

ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_DISPLAY *backdrop = NULL;
ALLEGRO_BITMAP* nought;
ALLEGRO_BITMAP* cross;
ALLEGRO_BITMAP* wall;
ALLEGRO_BITMAP* blank;
ALLEGRO_BITMAP* particle;
ALLEGRO_BITMAP* splash;
ALLEGRO_BITMAP* splash2;
ALLEGRO_BITMAP* standard;
ALLEGRO_BITMAP* random;
ALLEGRO_BITMAP* pass;
ALLEGRO_BITMAP* highlight;
ALLEGRO_BITMAP* movehighlight;
ALLEGRO_EVENT ev;
ALLEGRO_EVENT_QUEUE* event_queue;
ALLEGRO_MOUSE_STATE mousestate;
ALLEGRO_FONT *font;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_SAMPLE *bgm = NULL;

//ALLEGRO_COLOR color = al_map_rgba_f(1.0, 0.4, 0.6, 0); // <-- correct

const float FPS = 60;

int alpha = 128;

int score = 0;
int milliseconds = 0;
int seconds = 0;

int lastmove[2];

int resetmap[10][10];

int map[10][10];

int lastmousepos[2];
int currmousepos[2];

int validmove1[2];
int validmove2[2];
int validmove3[2];
int validmove4[2];

string loadtexturestring;
string points;
string gametimer;

int mode;

int clickx;
int clicky;

int mousex;
int mousey;

int moves;
int currlevel;
char nextLeveld1 = '0';
char nextLeveld2 = '0';
int multiplesOfTen = 0;

int tilesize = 100;



	int drawbufferx = 0;
	int drawbuffery = 0;

	template <class T>
	string to_string (const T& t)
	{
		stringstream ss;
		ss << t;
		return ss.str();
	}



	void resetMap()
	{
		memcpy(map,resetmap,sizeof map);
		PlaySound(L"restart.wav", NULL, SND_ASYNC | SND_FILENAME);
	}

	bool checkwin()
	{
		int numnought = 0;

		for(int countline = 0; countline <= 9; countline++)
		{
			for(int count = 0;count <= 9; count++)
			{
				if(map[countline][count] == 1)
				{
					numnought ++;
				}
			}
		}
		if(numnought >= 1)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	bool canmove()
	{
		
		int movey = lastmove[0];
		int movex = lastmove[1];
		int checkblank[2] = {0,0};
		int possmoves = 0;

		if(map[movey+1][movex] == 1)
		{
			possmoves ++;
			validmove1[0] = movey+1;
			validmove1[1] = movex;
		}
		else if(map[movey+1][movex] == 0 && map[movey+2][movex] == 1)
		{
				possmoves ++;
				validmove1[0] = movey+2;
				validmove1[1] = movex;
		}
		else if(map[movey+1][movex] == 2)
		{
			checkblank[0] = movey+1;
			checkblank[1] = movex;
			while(map[checkblank[0]][checkblank[1]] == 2)
			{
				checkblank[0] = checkblank[0]++;
				checkblank[1] = checkblank[1];
			}
			if(map[checkblank[0]][checkblank[1]] == 1)
			{
				possmoves ++;
				validmove1[0] = checkblank[0];
				validmove1[1] = checkblank[1];
			}
			if(map[checkblank[0]++][checkblank[1]] == 0)
			{
				possmoves = possmoves;
			}
			if(map[checkblank[0]][checkblank[1]] == 3)
			{
				possmoves = possmoves;
			}
		}

		checkblank[0] = 0;
		checkblank[1] = 0;

		if(map[movey-1][movex] == 1)
		{
			possmoves++;
			validmove2[0] = movey-1;
			validmove2[1] = movex;
		}
		else if(map[movey-1][movex] == 2)
		{
			checkblank[0] = movey-1;
			checkblank[1] = movex;
			while(map[checkblank[0]][checkblank[1]] == 2)
			{
				checkblank[0] = checkblank[0]--;
				checkblank[1] = checkblank[1];
			}
			if(map[checkblank[0]][checkblank[1]] == 1)
			{
				possmoves ++;
				validmove2[0] = checkblank[0];
				validmove2[1] = checkblank[1];
			}
			if(map[checkblank[0]][checkblank[1]] == 0)
			{

			}
			if(map[checkblank[0]][checkblank[1]] == 3)
			{
				
			}
		}
		else if(map[movey-1][movex] == 0 && map[movey-2][movex] == 1)
		{
				possmoves ++;
				validmove2[0] = movey-2;
				validmove2[1] = movex;
		}

		checkblank[0] = 0;
		checkblank[1] = 0;

		if(map[movey][movex+1] == 1)
		{
			possmoves++;
			validmove3[0] = movey;
			validmove3[1] = movex+1;
		}
		else if(map[movey][movex+1] == 2)
		{
			checkblank[0] = movey;
			checkblank[1] = movex+1;
			while(map[checkblank[0]][checkblank[1]] == 2)
			{
				checkblank[0] = checkblank[0];
				checkblank[1] = checkblank[1]++;
			}
			if(map[checkblank[0]][checkblank[1]] == 1)
			{
				possmoves ++;
				validmove3[0] = checkblank[0];
				validmove3[1] = checkblank[1];
			}
			if(map[checkblank[0]][checkblank[1]] == 0)
			{

			}
			if(map[checkblank[0]][checkblank[1]] == 3)
			{
				
			}
		}
		else if(map[movey][movex+1] == 0 && map[movey][movex+2] == 1)
		{
				possmoves ++;
				validmove3[0] = movey;
				validmove3[1] = movex+2;

		}

		checkblank[0] = 0;
		checkblank[1] = 0;
		
		if(map[movey][movex-1] == 1)
		{
			possmoves++;
			validmove4[0] = movey;
			validmove4[1] = movex-1;
		}
		else if(map[movey][movex-1] == 2)
		{
			checkblank[0] = movey;
			checkblank[1] = movex-1;
			while(map[checkblank[0]][checkblank[1]] == 2)
			{
				checkblank[0] = checkblank[0];
				checkblank[1] = checkblank[1]--;
			}
			if(map[checkblank[0]][checkblank[1]] == 1)
			{
				possmoves ++;
				validmove4[0] = checkblank[0];
				validmove4[1] = checkblank[1];
			}
			if(map[checkblank[0]][checkblank[1]] == 0)
			{

			}
			if(map[checkblank[0]][checkblank[1]] == 3)
			{
				
			}
		}
		else if(map[movey][movex-1] == 0 && map[movey][movex-2] == 1)
		{
				possmoves ++;
				validmove4[0] = movey;
				validmove4[1] = movex-2;
		}	

		checkblank[0] = 0;
		checkblank[1] = 0;

		if(possmoves >= 1)
		{
			cout << endl << "POSSIBLE MOVES: " << possmoves << endl << endl;
			return true;
		}
		else
		{
			return false;
		}
	}

	bool validmove(int clicky, int clickx)
	{
		if(clicky == validmove1[0] && clickx == validmove1[1])
		{
			return true;
		}
		else if(clicky == validmove2[0] && clickx == validmove2[1])
		{
			return true;
		}
		else if(clicky == validmove3[0] && clickx == validmove3[1])
		{
			return true;
		}
		else if(clicky == validmove4[0] && clickx == validmove4[1])
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	void randommap()
	{
		srand ( time(NULL) );
		for(int countline = 0; countline <= 9; countline++)
		{
			for(int count = 0;count <= 9; count++)
			{

				map[countline][count] = rand() % 2 + 1 ;
			}
		}
		for(int forceborder = 0; forceborder <= 9; forceborder++)
		{
			map[forceborder][0] = 3;
			map[forceborder][9] = 3;
			map[0][forceborder] = 3;
			map[9][forceborder] = 3;

		}
		memcpy(resetmap,map,sizeof resetmap);
	}

	void loadmap(char mapnumd1, char mapnumd2)
	{
		 int x;
		 ifstream inFile;
		 string nextLevel = "maps\\tech\\levelXX.txt";
			
		 nextLevel[15] = mapnumd1;
		 nextLevel[16] = mapnumd2;
		 cout << nextLevel.c_str();

		 inFile.open(nextLevel.c_str(), ifstream::in | ifstream::binary);
		 if (!inFile.is_open()) 
		 {
				cout << "Unable to open file";
				//exit(1); // terminate with error
		}
		 else if(inFile.is_open())
		 {
			 cout << "File opened!";
		 }


		for(int countline = 0; countline <= 9; countline++)
		{
			for(int count = 0;count <= 9; count++)
			{
				inFile >> x;
				map[countline][count] = x;
			}
		}
		memcpy(resetmap,map,sizeof resetmap);
		inFile.close();
	}

	void redraw()
	{
		drawbufferx = 0;
		drawbuffery = 0;
		for(int countline = 0; countline <= 9; countline++)
		{
			for(int count = 0;count <= 9; count++)
			{
				if(map[countline][count] == 1)
				{
					al_draw_bitmap(cross,drawbufferx,drawbuffery,0);
					drawbufferx += 50;
				}
				else if(map[countline][count] == 0)
				{
					al_draw_bitmap(nought,drawbufferx,drawbuffery,0);
					drawbufferx += 50;
				}
				else if(map[countline][count] == 3)
				{
					al_draw_bitmap(wall,drawbufferx,drawbuffery,0);
					drawbufferx += 50;
				}
				else if(map[countline][count] == 2)
				{
					al_draw_bitmap(blank,drawbufferx,drawbuffery,0);
					drawbufferx += 50;
				}
			}
			drawbufferx = 0;
			drawbuffery += 50;
		}
		drawbufferx = 0;
		drawbuffery = 0;
	}

	void loadtextures(string mapset)
	{
		loadtexturestring = "maps\\XXXX\\nought.png";
		loadtexturestring[5] = mapset[0];
		loadtexturestring[6] = mapset[1];
		loadtexturestring[7] = mapset[2];
		loadtexturestring[8] = mapset[3];
		nought = al_load_bitmap(loadtexturestring.c_str());
		cout << "Loaded texture: " << loadtexturestring.c_str() << endl;
		loadtexturestring = "maps\\XXXX\\cross.png";
		loadtexturestring[5] = mapset[0];
		loadtexturestring[6] = mapset[1];
		loadtexturestring[7] = mapset[2];
		loadtexturestring[8] = mapset[3];
		cross = al_load_bitmap(loadtexturestring.c_str());
		cout << "Loaded texture: " << loadtexturestring.c_str() << endl;
		loadtexturestring = "maps\\XXXX\\wall.png";
		loadtexturestring[5] = mapset[0];
		loadtexturestring[6] = mapset[1];
		loadtexturestring[7] = mapset[2];
		loadtexturestring[8] = mapset[3];
		wall = al_load_bitmap(loadtexturestring.c_str());
		cout << "Loaded texture: " << loadtexturestring.c_str() << endl;
		loadtexturestring = "maps\\XXXX\\blank.png";
		loadtexturestring[5] = mapset[0];
		loadtexturestring[6] = mapset[1];
		loadtexturestring[7] = mapset[2];
		loadtexturestring[8] = mapset[3];
		blank = al_load_bitmap(loadtexturestring.c_str());
		cout << "Loaded texture: " << loadtexturestring.c_str() << endl;
		splash = al_load_bitmap("DoWhile.bmp");
		splash2 = al_load_bitmap("Arctonyx.bmp");
		particle = al_load_bitmap("maps\\tech\\particle.bmp");
		highlight = al_load_bitmap("highlight.png");
		movehighlight = al_load_bitmap("movehighlight.png");
		pass = al_load_bitmap("buttons\\pass.bmp");
	}

	void mainmenu()
	{
		standard = al_load_bitmap("buttons\\standard.bmp");
		random = al_load_bitmap("buttons\\random.bmp");
		al_draw_bitmap(standard,0,0,0);
		al_draw_bitmap(random,0,250,0);
		al_flip_display();

	  // al_play_sample(bgm, 1.0, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL);


       while(true)
	   {

		event_queue = al_create_event_queue();
		al_register_event_source(event_queue, al_get_mouse_event_source());
		al_wait_for_event(event_queue, &ev);

		   if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
		   {
			   if(ev.mouse.y < 250)
			   {
				   mode = 2;
				   break;
			   }
			   else if(ev.mouse.y > 250)
			   {
				   mode = 1;
				   break;
				   	HWND hWnd = GetConsoleWindow(); 
				   	ShowWindow( hWnd, SW_SHOW );
				   cout << mode;
			   }
		   }
	   }
	   al_stop_samples();
	}

	void printscore()
	{
	 points = to_string(score);
	 al_clear_to_color(al_map_rgb(0,0,0));
	 al_draw_text(font, al_map_rgb(255,255,255), 560, 10, ALLEGRO_ALIGN_RIGHT, "SCORE: ");
	 al_draw_text(font, al_map_rgb(255,255,255), 590, 10, ALLEGRO_ALIGN_RIGHT, points.c_str());
	 if(mode == 1)
	 {
	 gametimer = to_string(seconds);
	 al_draw_text(font, al_map_rgb(255,255,255), 585, 25, ALLEGRO_ALIGN_RIGHT, "TIME LEFT: ");
	 al_draw_text(font, al_map_rgb(255,255,255), 620, 25, ALLEGRO_ALIGN_RIGHT, gametimer.c_str());
	 }
	}

	void countdown(void *arg)
	{
		while(true)
		{
		seconds -= 1;
		Sleep(1000);
		//printscore();
		}
	}

	void particleEffect()
	{
		int particleposx = ev.mouse.x;
		int particleposy = ev.mouse.y;
		for(int count = 0; count <= 10; count ++)
		{
			al_wait_for_event(event_queue, &ev);
			if(ev.type == ALLEGRO_EVENT_TIMER)
			{
		    al_draw_bitmap(particle,particleposx,particleposy,0);
			al_flip_display();
			particleposx ++;
			particleposy ++;
			}
		}
	}


int main(int argc, char** argv)
{
    al_init();
    al_init_image_addon();
	al_init_font_addon();
	al_init_ttf_addon();
	al_install_audio();
	al_init_acodec_addon();
	al_reserve_samples(1);
	bgm = al_load_sample( "bgm.wav");

	string texturechoice;
	int textureinput;
	cout << "Select Texture Set (1 Tech, 2 Dark, 3 Nature): ";
	cin >> textureinput;
	cout << endl << endl;
	if (textureinput == 1)
	{
		loadtextures("tech");
	}
	else if (textureinput == 2)
	{
		loadtextures("dark");
	}
	else if (textureinput == 3)
	{
		loadtextures("natu");
	}


	al_set_new_display_flags(ALLEGRO_NOFRAME);
	display = al_create_display(500, 500);

	HWND hWnd = GetConsoleWindow();  
	ShowWindow( hWnd, SW_HIDE );  

	al_draw_bitmap(splash,0,0,0);
	al_flip_display();

	Sleep(3000);

	al_draw_bitmap(splash2,0,0,0);
	al_flip_display();

	Sleep(3000);

	al_install_mouse();

	mainmenu();

	al_destroy_display(display);
	display = al_create_display(700,500);

	timer = al_create_timer(1.0 / FPS);

	font = al_load_ttf_font("pirulen.ttf",10,0);

	printscore();

	al_flip_display();


	currlevel = 0;
	
	if(mode == 1)
	{
		randommap();
		
		al_draw_bitmap(pass,500,300,0);
		redraw();
        al_flip_display();
		seconds = 240;
		_beginthread( countdown, 0, (void*)12 );
		

		while(true)
		{
		printscore();

		al_clear_to_color(al_map_rgb(0,0,0));
		event_queue = al_create_event_queue();
		al_register_event_source(event_queue, al_get_timer_event_source(timer));
		al_register_event_source(event_queue, al_get_mouse_event_source());
		al_wait_for_event(event_queue, &ev);

        if(ev.type == ALLEGRO_EVENT_MOUSE_AXES && ev.mouse.x < 500 && ev.mouse.y < 500)
		{
			mousex = ev.mouse.x/50;
			mousey = ev.mouse.y/50;
			currmousepos[0] = mousey*50;
			currmousepos[1] = mousex*50;
			if(currmousepos[0] != lastmousepos[0] || currmousepos[1] != lastmousepos[1])
			{
				if(map[currmousepos[0]/50][currmousepos[1]/50] == 1)
				{
				printscore();
				redraw();
			
				al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);

				al_draw_bitmap(highlight,mousex*50,mousey*50,0);
				al_draw_bitmap(pass,500,300,0);
			
			
				al_flip_display();
				}
			}
			lastmousepos[0] = currmousepos[0];
			lastmousepos[1] = currmousepos[1];
		}

		if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
		{
			clickx = ev.mouse.x/50;
			clicky = ev.mouse.y/50;
			
			if(ev.mouse.x > 500 && ev.mouse.y > 300)
			{
				//randommap();
				moves = 0;
				lastmove[0] = 0;
				lastmove[1] = 0;
				system("CLS");
				redraw();
				al_draw_bitmap(pass,500,300,0);
				al_flip_display();
			}
			else if(ev.mouse.x < 500 && ev.mouse.y < 500 && map[clicky][clickx] == 1)
			{
				if(validmove(clicky,clickx) == true || moves == 0)
				{
					map[clicky][clickx] = 0;
					PlaySound(L"bleep.wav", NULL, SND_ASYNC | SND_FILENAME);
					//particleEffect();
					if(moves >= 1)
					{
					map[lastmove[0]][lastmove[1]] = 3;
					}
					lastmove[0] = clicky;
					lastmove[1] = clickx;
					moves ++;
					score += 20;
					printscore();
				}
				else
				{
					cout << endl << endl << "You cannot move there" << endl << endl;
				}
				redraw();
				al_draw_bitmap(pass,500,300,0);
				al_flip_display();
			}
			else if(ev.mouse.x < 500 && ev.mouse.y < 500 && map[clicky][clickx] == 2)
			{
				ShowWindow( hWnd, SW_SHOW ); 
				break;
			}

			cout << "LAST MOVE: " << lastmove[0] << "," << lastmove[1] << endl;
			if(checkwin() == true)
			{
				cout << endl << endl << "YOU WIN!";
				PlaySound(L"win.wav", NULL, SND_ASYNC | SND_FILENAME);
				Sleep(2000);
				system("CLS");
				randommap();
				resetMap();
				moves = 0;
				lastmove[0] = 0;
				lastmove[1] = 0;
				score += 500;
				printscore();
				redraw();
				al_draw_bitmap(pass,500,300,0);
				al_flip_display();
			}
			else if(canmove() == false)
			{
				cout << endl << endl << "YOU LOSE" << endl << endl;
				PlaySound(L"fail.wav", NULL, SND_ASYNC | SND_FILENAME);
				Sleep(2000);
				randommap();
				moves = 0;
				lastmove[0] = 0;
				lastmove[1] = 0;
				system("CLS");
				printscore();
				redraw();
				al_draw_bitmap(pass,500,300,0);
				al_flip_display();
			}
			cout << endl;



		}
		}
	}

	else if(mode == 2)
	{
	loadmap('0','0');

	redraw();
    al_flip_display();
	
	al_install_mouse();

	currlevel = 0;

		while(true)
		{
		al_clear_to_color(al_map_rgb(0,0,0));
		event_queue = al_create_event_queue();
		al_register_event_source(event_queue, al_get_mouse_event_source());
		al_wait_for_event(event_queue, &ev);

        if(ev.type == ALLEGRO_EVENT_MOUSE_AXES && ev.mouse.x < 500 && ev.mouse.y < 500)
		{
			mousex = ev.mouse.x/50;
			mousey = ev.mouse.y/50;
			currmousepos[0] = mousey*50;
			currmousepos[1] = mousex*50;
			if(currmousepos[0] != lastmousepos[0] || currmousepos[1] != lastmousepos[1])
			{

			printscore();
			redraw();
			
			al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);

			al_draw_bitmap(highlight,mousex*50,mousey*50,0);
			al_draw_bitmap(pass,500,300,0);
			
			
			al_flip_display();
			}
			lastmousepos[0] = currmousepos[0];
			lastmousepos[1] = currmousepos[1];
		}

		if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
		{
			clickx = ev.mouse.x/50;
			clicky = ev.mouse.y/50;

			if(map[clicky][clickx] == 1)
			{
				if(validmove(clicky,clickx) == true || moves == 0)
				{
					map[clicky][clickx] = 2;
					PlaySound(L"bleep.wav", NULL, SND_ASYNC | SND_FILENAME);
					if(moves >= 1)
					{
					map[lastmove[0]][lastmove[1]] = 0;
					}
					lastmove[0] = clicky;
					lastmove[1] = clickx;
					moves ++;
				}
				else
				{
					cout << endl << endl << "You cannot move there" << endl << endl;
				}
				redraw();
				al_flip_display();
			}
			else if(map[clicky][clickx] == 2)
			{
				ShowWindow( hWnd, SW_SHOW ); 
				break;
			}

			cout << "LAST MOVE: " << lastmove[0] << "," << lastmove[1] << endl;
			if(checkwin() == true)
			{
				cout << endl << endl << "YOU WIN!";
				PlaySound(L"win.wav", NULL, SND_ASYNC | SND_FILENAME);
				Sleep(2000);
				currlevel++;
				if(currlevel == 10)
				{
					currlevel = 0;
					multiplesOfTen++;
					nextLeveld1 = multiplesOfTen + '0';
				}
				nextLeveld2 = currlevel + '0';
				system("CLS");
				loadmap(nextLeveld1, nextLeveld2);
				//break;
				resetMap();
				moves = 0;
				lastmove[0] = 0;
				lastmove[1] = 0;
				redraw();
				al_flip_display();
			}
			else if(canmove() == false)
			{
				cout << endl << endl << "YOU LOSE" << endl << endl;
				PlaySound(L"fail.wav", NULL, SND_ASYNC | SND_FILENAME);
				Sleep(2000);
				resetMap();
				moves = 0;
				lastmove[0] = 0;
				lastmove[1] = 0;
				system("CLS");
				redraw();
				al_flip_display();
			}
			cout << endl;



		}
	}
		



	}

		al_destroy_display(display);
		system("PAUSE");
		return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.