The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Game Development
|
[Allegro & C] buffer problem
Discuss [Allegro & C] buffer problem in the Game Development forum on Dev Shed. [Allegro & C] buffer problem Game Development forum covering non language specific programming - game creation, design, modding, theories and math. A place for developers and gamers of all levels to discuss and debate all things involved in game creation and modding.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 10th, 2007, 05:27 PM
|
|
Registered User
|
|
Join Date: Aug 2007
Posts: 5
Time spent in forums: 37 m 35 sec
Reputation Power: 0
|
|
|
[Allegro & C] buffer problem
I started learning c like 3 or 4 days ago, and i followed some tutorials i found and i could make some simple games, anyways, now i wanna make a simple space ships game, and i have problems with the ship and the backround, the bg is just pixels moving simulating stars, and the ship is a bmp, when i run only the stars it works perfectly, also if a run only the ship, but i need both so i draw the sprites into a buffer bitmap. anyways now the stars works weird.. only some of the move the others keep at y = 0.. or there is an error displaying them..
here its an screenshot
and here it is the source code
Code:
#include <allegro.h>
#include <time.h>
/* Functions signatures */
void init();
void deinit();
void moveStars();
void playerFunc();
/* Constants */
#define WINDOW_W 640
#define WINDOW_H 480
#define TOTAL_STARS 200
#define BG_SPEED 1
/* Global Varialbes */
int moving = 0; /* Is the bg moving? Used in: moveStars() */
int playerPosX = 320; /* Player x position Used in: PlayerFunc() */
int playerPosY = 240; /* Player y position Used in: PlayerFunc() */
/* Bitmaps */
BITMAP *buffer;
BITMAP *player;
BITMAP *enemy;
int main() {
init();
/* Load BMPS */
player = load_bitmap("images/player.bmp",NULL);
enemy = load_bitmap("images/enemy.bmp",NULL);
/* Prepare buffer */
buffer = create_bitmap(WINDOW_W,WINDOW_H);
while (!key[KEY_ESC])
{
moveStars();
playerFunc();
}
deinit();
return 0;
}
END_OF_MAIN()
void init() {
int depth, res;
allegro_init();
depth = desktop_color_depth();
if (depth == 0) depth = 32;
set_color_depth(depth);
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, WINDOW_W, WINDOW_H, 0, 0);
if (res != 0) {
allegro_message(allegro_error);
exit(-1);
}
install_timer();
install_keyboard();
install_mouse();
}
void deinit() {
clear_keybuf();
}
void moveStars(){
int i;
struct coord
{
int x[TOTAL_STARS];
int y[TOTAL_STARS];
} pos;
if(moving == 0)
{
srand((unsigned)time(0));
for(i = 0; i < TOTAL_STARS; i++)
{
pos.x[i] = rand()%WINDOW_W;
pos.y[i] = rand()%WINDOW_H;
}
moving = 1;
}
else if(moving == 1)
{
acquire_screen();
for(i = 0; i < TOTAL_STARS; i++)
{
putpixel(buffer, pos.x[i], pos.y[i], makecol(0,0,0));
if(pos.y[i] > WINDOW_H)
pos.y[i] = 2;
else
pos.y[i]+=BG_SPEED;
putpixel(buffer, pos.x[i], pos.y[i], makecol(255,255,255));
}
draw_sprite(screen,buffer,0,0);
release_screen();
}
}
void playerFunc(){
acquire_screen();
if(key[KEY_RIGHT] && playerPosX <= WINDOW_W-40)
playerPosX++;
else if(key[KEY_LEFT] && playerPosX >= 0)
playerPosX--;
else if(key[KEY_UP] && playerPosY >= 0)
playerPosY--;
else if(key[KEY_DOWN] && playerPosY <= WINDOW_H-40)
playerPosY++;
draw_sprite(buffer,player,playerPosX,playerPosY);
draw_sprite(screen,buffer,0,0);
release_screen();
}
thanks in advance 
|

October 6th, 2007, 08:25 PM
|
|
|
Your drawing to a buffer but not flipping the buffer to the screen. Also don't draw anything to the screen. Draw to a buffer and then blit the buffer to the screen.
As for the stars staying at y = 0; You might want to install the install_timer and srand(time(NULL)); I will give your code another look to see what else could cause that.
Also create_bitmap for your buffer is not how you make a bitmap for controlling video use:
buffer = create_video_bitmap(SCREEN_W, SCREEN_H);
You can use page flipping. With flipping you don't need to acquire_screen(); or release_screen();
draw_sprite(screen, buffer, 0, 0); really isn't that effective.
Blit is not only faster but the proper way to do it. Any now here is page flipping. Or the no flicker method.
Code:
// variables you will need
BITMAP *buffer, *buffer2, *activepage;
int main() //Make your main
{
/* allegro_init(); //Init allegro and all the video keyb mouse
set_color_depth(32);
install_mouse();
install_keyboard();
install_timer(); //Timer functions
srand(time(NULL)); //randoms
int ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0); //INIT Video and check for errors
if (ret != 0)
{
allegro_message(allegro_error);
return;
}*/
//Now for your buffer and active page
buffer = create_video_bitmap(SCREEN_W, SCREEN_H);
buffer2 = create_video_bitmap(SCREEN_W, SCREEN_H);
activepage = buffer2;
while(ingameloop == TRUE)
{
//Draw your stars
//Draw your ship
flip(); //and thats the magic
}
}
END_OF_MAIN() //Notice no ;
// Function you will need
void flip()
{
show_video_bitmap(activepage); //Page flipping :) fun stuff
if (activepage == buffer)
activepage = buffer2;
else
activepage = buffer;
clear_to_color(activepage, makecol(0, 0, 0));
}
EDIT: Ok I went over your code.
Code:
int i;
struct coord
{
int x[TOTAL_STARS];
int y[TOTAL_STARS];
} pos;
are being declared every time you call moveStars();
Here are the changes that I made you should be able to compile and run right away
Code:
#include <allegro.h>
#include <time.h>
#define WINDOW_W 640
#define WINDOW_H 480
#define TOTAL_STARS 200
#define BG_SPEED 2
//removed moving cause it is no longer needed
//int moving = 0; /* Is the bg moving? Used in: moveStars() */
int playerPosX = 320; /* Player x position Used in: PlayerFunc() */
int playerPosY = 240; /* Player y position Used in: PlayerFunc() */
//changed put the i and struct in the correct location
int i;
struct coord
{
int x[TOTAL_STARS];
int y[TOTAL_STARS];
} pos;
/* Bitmaps */
//changed added buffers
BITMAP *buffer, *buffer2, *activepage;
BITMAP *player;
BITMAP *enemy;
/* Functions signatures */
void init()
{
allegro_init();
//changed removed the depth and just set it to 32 and removed the set_desktop_depth();
set_color_depth(32);
//changed declares the video and the check at the same time
if (int res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, WINDOW_W, WINDOW_H, 0, 0) != 0)
{
allegro_message(allegro_error);
exit(-1);
}
//changed create_video_bitmap for buffer buffer2 and activepage
buffer = create_video_bitmap(SCREEN_W, SCREEN_H);
buffer2 = create_video_bitmap(SCREEN_W, SCREEN_H);
activepage = buffer2;
install_timer();
install_keyboard();
install_mouse();
//changed this is the proper spot for srand(time(NULL));
srand(time(NULL));
}
void deinit()
{
clear_keybuf();
//changed added destroy_? for your buffers to eliminate memory leaks
destroy_bitmap(buffer);
destroy_bitmap(buffer2);
destroy_bitmap(activepage);
destroy_bitmap(player);
destroy_bitmap(enemy);
}
void flip()
{
//changed here is the page flipping.
show_video_bitmap(activepage); //Page flipping :) fun stuff
if (activepage == buffer)
activepage = buffer2;
else
activepage = buffer;
clear_to_color(activepage, makecol(0, 0, 0));
}
void moveStars()
{
for(i = 0; i < TOTAL_STARS; i++)
{
//putpixel(activepage, pos.x[i], pos.y[i], makecol(0,0,0));
//changed removed deleting the star before you move it cause it is no longer needed
if(pos.y[i] > WINDOW_H)
pos.y[i] = 2;
else
pos.y[i]+=BG_SPEED;
putpixel(activepage, pos.x[i], pos.y[i], makecol(255,255,255));
}
}
void playerFunc()
{
if(key[KEY_RIGHT] && playerPosX <= WINDOW_W-40)
playerPosX++;
else if(key[KEY_LEFT] && playerPosX >= 0)
playerPosX--;
else if(key[KEY_UP] && playerPosY >= 0)
playerPosY--;
else if(key[KEY_DOWN] && playerPosY <= WINDOW_H-40)
playerPosY++;
draw_sprite(activepage,player,playerPosX,playerPosY);
//changed buffer to activepage
}
int main()
{
init();
/* Load BMPS */
player = load_bitmap("images/player.bmp",NULL);
enemy = load_bitmap("images/enemy.bmp",NULL);
for(i = 0; i < TOTAL_STARS; i++)
{
pos.x[i] = rand() % WINDOW_W;
pos.y[i] = rand() % WINDOW_H;
}
while (!key[KEY_ESC])
{
moveStars();
playerFunc();
flip();
}
deinit();
return 0;
}
END_OF_MAIN()
Btw I added page flipping for you so you don't have to figure it out. There was nothing wrong with the stars but that int i and struct call every run was messing with something. Also remember if you create_bitmap or create_video_bitmap or load datafile you need to destroy/release it otherwise you will have memory leaks in your program/game.
//changed tells you what I changed
|

October 6th, 2007, 10:10 PM
|
|
|
Your moving along and learning at a good rate. Soon you will be working with animated sprites and sounds but worry about that stuff after your game concept is done.
Your concept version (basically what you just wrote) looks like my original FallingBall game. Which can be found on this forum which is written in allegro.
FallingBall v1.23
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|