Mail Archives: djgpp/2003/03/15/04:00:13
I have DJGPP under win2k and it works fine with Allegro too.
I encountered this weird behaviour:
the following works very well:
---
/* muovere un oggetto con il mouse */
#include <stdio.h>
#include <allegro.h>
// #include "0632.h"
// #include "0632c.h"
// Header for the Vivace example 51single
extern int end_game;
extern BITMAP *dblbuffer;
int main (void);
void init(void);
void input(void);
void process(void);
void output(void);
void shutdown(void);
void show_double_buffer(void);
// fine 0632.h
// Header containing standard reusable functions to move a circle –
0.632c.h
typedef struct circle_t CIRCLE_T;
struct circle_t {
int x,y,r; // x, y, radius
int col; // colour
int xs,ys; // x speed, y speed
int dx,dy; // drawn x, drawn y
};
CIRCLE_T *circle_init(int new_x, int new_y, int new_radius,
int new_col, int new_xs, int new_ys);
void circle_destroy (CIRCLE_T *circle);
void circle_draw (CIRCLE_T *circle, BITMAP *bmp);
void circle_update (CIRCLE_T *circle);
void circle_erase (CIRCLE_T *circle, BITMAP *bmp);
// fine 0632c.h
CIRCLE_T *circle_init(int new_x, int new_y, int new_radius,
int new_col, int new_xs, int new_ys)
{
CIRCLE_T *temp;
// Allocate memory for the circle. If unsuccessful, return NULL.
temp = malloc(sizeof(CIRCLE_T));
if (!temp)
return NULL;
temp->x = new_x;
temp->y = new_y;
temp->r = new_radius;
temp->col = new_col;
temp->xs = new_xs;
temp->ys = new_ys;
temp->dx = temp->dy = 0;
return temp;
}
void circle_destroy(CIRCLE_T * circle)
{
if (circle)
free(circle);
}
void circle_draw(CIRCLE_T * circle, BITMAP * bmp)
{
circlefill(bmp, circle->x, circle->y, circle->r, circle->col);
circle->dx = circle->x;
circle->dy = circle->y;
}
void circle_update(CIRCLE_T * circle)
{
// First we update the position of the circle...
circle->x += circle->xs;
circle->y += circle->ys;
// Now we see if it reached the maximum frame border
if (circle->x >= SCREEN_W)
circle->x = SCREEN_W;
if (circle->y >= SCREEN_H)
circle->y = SCREEN_H;
if (circle->x < 1)
circle->x = 0;
if (circle->y < 1)
circle->y = 0;
}
void circle_erase(CIRCLE_T * circle, BITMAP * bmp)
{
circlefill(bmp, circle->dx, circle->dy, circle->r, 0);
}
// fine 0632c.c
int end_game; /* flag we'll set to end the game */
int pressed_fire_button; // This will help with the color feature
BITMAP *dblbuffer;
CIRCLE_T *moving_circle;
int main(void)
{
allegro_init(); /* initialise the Allegro library */
init(); /* initialise the game */
end_game = 0; /* reset flag */
do { /* loop */
input(); /* get input */
process(); /* process it */
output(); /* give output */
} while (end_game == 0); /* until the flag is set */
shutdown(); /* shut down anything that needs it */
allegro_exit(); /* just for luck */
return 0; /* tell the OS that all went well */
}
END_OF_MAIN()
void init(void)
{
int i;
install_keyboard();
i = install_mouse();
if (i == -1) {
printf("Sorry, didn't detect any mouse\n");
exit(1);
}
if (set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) < 0) {
printf("%s\n", allegro_error);
exit(2);
}
dblbuffer = create_bitmap(SCREEN_W, SCREEN_H);
if (dblbuffer == NULL) {
allegro_exit();
printf("Sorry, not enough memory");
exit(3);
}
clear(dblbuffer);
moving_circle = circle_init(SCREEN_W / 2, SCREEN_H / 2, 50, 3, 0,
0);
if (moving_circle == NULL) {
allegro_exit();
printf("Sorry, not enough memory");
exit(4);
}
pressed_fire_button = 0;
circle_draw(moving_circle, dblbuffer);
show_double_buffer();
}
void input(void)
{
int dx, dy;
if (key[KEY_ESC])
end_game++;
get_mouse_mickeys(&dx, &dy);
if ((mouse_b & 1) != pressed_fire_button) {
pressed_fire_button = moving_circle->col;
while (pressed_fire_button == moving_circle->col)
moving_circle->col = rand() % 14;
pressed_fire_button = mouse_b & 1;
}
moving_circle->xs += dx;
moving_circle->ys += dy;
}
void process(void)
{
circle_update(moving_circle);
}
void output(void)
{
circle_erase(moving_circle, dblbuffer);
circle_draw(moving_circle, dblbuffer);
show_double_buffer();
}
void shutdown(void)
{
circle_destroy(moving_circle);
destroy_bitmap(dblbuffer);
}
void show_double_buffer(void)
{
vsync();
blit(dblbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}
---------
(END FIRST PRGM)
then I downloaded the following for the mastermind game and the mouse
won't move correctly !!! (i have the .dat and the .h correctly loaded
and grabbed)
/*------------------------MASTERMIND-----------------By Arthur
Brochard*/
#include <allegro.h>
#include <stdlib.h>
#include <time.h>
#include "mastermind.h"
DATAFILE *datafile;
BITMAP *buffer;
int code[4]; /*the secret code you
have to guess*/
int tab[8][4]; /*your 8 attempts*/
int colors[6] = { 83, 82, 81, 53, 86, 54 }; /*the codes of the 6
colors in the palette*/
int bons[8]; /*for each guess, the
number of colors which are
in the right position*/
int mal[8]; /*for each guess, the
number of colors which are
in the wrong position*/
int essai=0; /*number of the attempt
0 = first attempt*/
int trou=0; /*number of the hole 0
= first hole*/
int fin=0; /* if the game is over,
fin = 1 */
void nouveau() /*Initialization*/
{
int i,j;
fin = trou = essai = 0;
for(i=0;i<8;i++)
{
bons[i]=mal[i]=0;
for(j=0;j<4;j++)
tab[i][j]=-1;
}
srand((unsigned) time(NULL) );
for( i=0;i<4;i++ )
code[i] = rand() % 6;
}
void check() /*checks how many
mistakes there are*/
{
int i, j;
int t[4];
int c[4];
for(i=0;i<4;i++)
{
t[i] = tab[essai][i];
c[i] = code[i];
if( t[i] == c[i] )
{
bons[essai]++;
t[i] = c[i] = -i-10;
}
}
if(bons[essai] == 4)
goto end;
for( i=0;i<4;i++ )
{
for(j=0;j<4;j++)
{
if( i == j )
continue;
if(t[i] == c[j])
{
mal[essai]++;
t[i] = c[j] = -i-1;
}
}
}
end : ;
}
void draw() /*Draws everything on the screen*/
{
int i, j;
blit( datafile[ECRAN].dat, buffer, 0, 0, 0, 0, 640, 480 );
if(!fin)
{
blit( datafile[FLECHE].dat, buffer, 0, 0, 7, (35+essai*50), 25, 11 );
circle( buffer, (80+trou*30), (40+essai*50), 11, 91 );
}
else
{
for(i=0;i<4;i++)
circlefill( buffer, (80+i*30), 440, 10, colors[(code[i])] );
if(fin == 2)
textprintf_centre(buffer, font, 470, 285, 91, "CONGRATULATIONS !");
else if(fin == 1)
{
textprintf_centre(buffer, font, 470, 285, 91, "GAME OVER !");
essai = 7;
}
}
for(i=0;i<(essai+1);i++)
{
if( (i != essai) || (fin) )
{
textprintf(buffer, font, 220, (35+i*50), 81, "%d", bons[i]);
textprintf(buffer, font, 240, (35+i*50), 91, "%d", mal[i]);
}
for(j=0;j<4;j++)
{
if(tab[i][j] >= 0)
circlefill( buffer, (80+j*30), (40+i*50), 10, colors[(tab[i][j])]
);
}
}
if( (mouse_x >= 410) && (mouse_x <= 530) )
{
if( (mouse_y >= 210) && (mouse_y <= 250) )
rect( buffer, 410, 210, 530, 250, 91);
else if( (mouse_y >= 320) && (mouse_y <= 360) )
rect( buffer, 410, 320, 530, 360, 91);
else if( (mouse_y >= 370) && (mouse_y <= 410) )
rect( buffer, 410, 370, 530, 410, 91);
}
show_mouse(buffer);
vsync();
blit( buffer, screen, 0, 0, 0, 0, 640, 480 );
}
main()
{
int i, cond=0;
if( allegro_init() != 0)
{
allegro_message("Failed to initialize Allegro\n");
exit(-1);
}
//
if (set_gfx_mode(GFX_SAFE, 640, 480, 0, 0) != 0) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("Unable to set any graphic mode\n%s\n",
allegro_error);
return 1;
}
//
// if( set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0 ) != 0 )
// {
// allegro_message("Failed to set graphics mode\n");
// exit(-1);
// }
if( install_mouse() == -1)
{
allegro_message("Failed to install the mouse\n");
exit(-1);
}
if( install_keyboard() != 0)
{
allegro_message("Failed to install the keyboard\n");
exit(-1);
}
datafile = load_datafile("mastermind.dat");
if (!datafile)
{
allegro_message("Error : can't load 'mastermind.dat'\n");
exit(-1);
}
set_pallete(datafile[PALETTE].dat);
buffer = create_bitmap(640, 480);
clear(buffer);
set_mouse_sprite(datafile[SOURIS].dat);
position_mouse(200, 200);
set_mouse_speed(1,1);
text_mode(-5);
nouveau();
while(1)
{
if(key[KEY_ESC])
{
allegro_exit();
exit(0);
}
draw();
if(mouse_b & 1)
{
/*If you select another hole*/
if( (mouse_x >= 70) && (mouse_x <= 180) )
{
if( (mouse_y >= (30+50*essai)) && (mouse_y <= (50+50*essai)) )
{
for(i=0;i<4;i++)
{
if( (mouse_x >= (70+30*i)) && (mouse_x <= (90+30*i)) )
trou = i;
}
}
}
/*If you select a color*/
else if( (mouse_x >= 420) && (mouse_x <= 520) )
{
if(mouse_x <= 440)
{
if( (mouse_y >= 130) && (mouse_y <= 150) )
{
tab[essai][trou] = 2;
if(trou < 3)
trou++;
}
else if( (mouse_y >= 170) && (mouse_y <= 190) )
{
tab[essai][trou] = 4;
if(trou < 3)
trou++;
}
}
else if(mouse_x >= 500)
{
if( (mouse_y >= 130) && (mouse_y <= 150) )
{
tab[essai][trou] = 5;
if(trou < 3)
trou++;
}
else if( (mouse_y >= 170) && (mouse_y <= 190) )
{
tab[essai][trou] = 1;
if(trou < 3)
trou++;
}
}
else if( (mouse_x >= 460) && (mouse_x <= 480) )
{
if( (mouse_y >= 130) && (mouse_y <= 150) )
{
tab[essai][trou] = 3;
if(trou < 3)
trou++;
}
else if( (mouse_y >= 170) && (mouse_y <= 190) )
{
tab[essai][trou] = 0;
if(trou < 3)
trou++;
}
}
}
/*If you click 'OK'*/
if( (mouse_x >= 410) && (mouse_x <= 530) )
{
if( (mouse_y >= 210) && (mouse_y <= 250) && (!fin) )
{
for(i=0;i<4;i++)
{
if( tab[essai][i] != -1 )
cond++;
}
if( cond == 4 )
{
check();
if(bons[essai] != 4)
{
essai++;
trou=0;
if( essai == 8 )
fin = 1;
}
else if( bons[essai] == 4 )
fin = 2;
}
cond = 0;
}
/*If you click 'New'*/
else if( (mouse_y >= 320) && (mouse_y <= 360) )
nouveau();
/*If you click 'Quit'*/
else if( (mouse_y >= 370) && (mouse_y <= 410) )
{
allegro_exit();
exit(0);
}
}
rest(220);
}
}
}
END_OF_MAIN();
---------------------------------------------
WHAT'S WRONG HERE ABOUT THE MOUSE MOVING ??
I really cannot see the problem here........
helen
[ bytes are just tears of programmers heaven]
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
- Raw text -