Qbasicnews.com

Full Version: ALERT - n00b to Allegro - ALERT
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Ah, I can't make anything of the reference. But I'll try. Smile
Thanks for all your help. Smile
Okay, so I coded a program to animate a circle bouncing around:
Code:
/* Compiled with Mingw32 */
#include <allegro.h>
#include <time.h>
#define RADIUS 5
#define CLR 2
int main()
{
allegro_init();
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,320,200,0,0);
install_keyboard();
int x=10;
int y=10;
int velocity=1;
time_t seconds;
while (!key[KEY_X])
{
  circle(screen,x,y,RADIUS,0);
  seconds=time(NULL);
  x += velocity;
  y += velocity;
  circle(screen,x,y,RADIUS,CLR);
  if (x==314 || y==194)
     velocity *= -1;
  int i;
  while ((time(NULL)-seconds) <= .2);
}
return 0;
}
END_OF_MAIN()
Two problems:
1) For some reason, it's delaying way way more than .02 seconds...but I googled delays, and it said to use the time() function, and the time_t struct...I think the problem is that when comparing the time(NULL)-seconds <= .2, it's comparing all members of the struct...but the website I saw said to do that. I dunno.
2) Every other iteration of the loop, it doesn't draw the complete circle, which I just can't understand.
You know allegro comes with timing routines, its in the manual

Code:
int i;
  while ((time(NULL)-seconds) <= .2);
I dont like how your doing the while loop a {} would be clearer, and is probably better coding practice. Second why int i here?

Also try adding a vsync() after the drawing instead of a timer.
Oops, forget the init i; - it's a typo.
Nath pointed it out...
Code:
while (condition) do_something();
Or
Code:
while (condition)
  do_something();
Or
Code:
while (condition)
{
do_something();
}
The very first example is what I use. Since ";" alone is a valid statement...it works: while(condition);.
I'll look up vsync();
Okay, I got vsync() down. Big Grin
Now, for my expiriment with loading BMPs!
WARNING - don't run the following code, it crashed my computer.
Code:
#include <allegro.h>
int main()
{
allegro_init();
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,320,200,0,0);
BITMAP *sprite;
RGB pal;
sprite=load_bmp("spr.bmp",&pal);
blit(sprite,screen,0,0,10,10,10,10);
while (!key[KEY_X]);
return 0;
}
END_OF_MAIN()
It crashed my computer. Sad I've saved the BMP with MSPAINT, in 256 color format...so what's wrong?
um, pal takes an array of RGB's (256 of em to be exact).

Your only suplying one.

Really read the documentation, all these questions are covered there. Print it out if neccessesary.
Er..I'm really bad at this graphics stuff. Tongue
So I would declare an array of Pals, 256 elements, and pass the address of the first element?
[edit]
Would this code work?
Code:
#include <allegro.h>
int main()
{
allegro_init();
install_keyboard();
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,320,200,0,0);
BITMAP *sprite;
RGB pal[256];
sprite=load_bmp("spr.bmp",pal);
blit(sprite,screen,0,0,10,10,10,10);
while (!key[KEY_X]);
return 0;
}
END_OF_MAIN()
You have a predefined type:

Code:
PALETTE my_pal;

Will allocate a 256 colour palete made up of RGBs Smile
Okaaay...But will the RGB pal[PAL_SIZE] work too?
yes.
Pages: 1 2 3