Game Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesGame Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 10th, 2007, 05:27 PM
fedekiller fedekiller is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 5 fedekiller User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old October 6th, 2007, 08:25 PM
0CIRCLE0 0CIRCLE0 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 65 0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level)0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level)0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level)0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level)0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level)0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level)0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 20 h 13 m 20 sec
Reputation Power: 89
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

Reply With Quote
  #3  
Old October 6th, 2007, 10:10 PM
0CIRCLE0 0CIRCLE0 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 65 0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level)0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level)0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level)0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level)0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level)0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level)0CIRCLE0 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 20 h 13 m 20 sec
Reputation Power: 89
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesGame Development > [Allegro & C] buffer problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap