I am using the allegro_primitives.h header file, and when I go to draw a rectangle with al_draw_filled_rectangle, and I move the rectangle with the keys, the outline of the rectangle in the direction that the rectangle is going is changing colors. Here's the code:
#include <allegro5\allegro5.h>
#include <allegro5\allegro_primitives.h>
#include <iostream>
#include "Globals.h"
using namespace std;
bool keys[5] = {false, false, false, false, false};
enum KEYS {UP, DOWN, LEFT, RIGHT, SPACE};
const int WINDOW_WIDTH = 600;
const int WINDOW_HEIGHT = 600;
int main()
{
bool done = false;
bool redraw = true;
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
if(!al_init())
return -1;
al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST);
display = al_create_display(WINDOW_WIDTH,WINDOW_HEIGHT);
if(!display)
return -1;
al_init_primitives_addon();
al_install_keyboard();
event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / 60);
if(!timer)
return -1;
if(!event_queue)
return -1;
al_register_event_source(event_queue,al_get_keyboard_event_source());
al_register_event_source(event_queue,al_get_timer_event_source(timer));
al_register_event_source(event_queue,al_get_display_event_source(display));
Character player;
Character lover;
player.x1 = 100;
player.x2 = player.x1 + 40;
player.y1 = (WINDOW_HEIGHT / 2) - 20;
player.y2 = player.y1 + 40;
lover.x1 = WINDOW_WIDTH - 140;
lover.x2 = lover.x1 + 40;
lover.y1 = (WINDOW_HEIGHT / 2) - 20;
lover.y2 = lover.y1 + 40;
al_start_timer(timer);
while(!done)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue,&ev);
if(ev.timer.source == timer)
{
if(keys[UP])
{
player.y1 -= 5;
player.y2 -= 5;
}
if(keys[DOWN])
{
player.y1 += 5;
player.y2 += 5;
}
if(keys[LEFT])
{
player.x1 -= 5;
player.x2 -= 5;
}
if(keys[RIGHT])
{
player.x1 += 5;
player.x2 += 5;
}
if(player.x1 <= 0)
{
player.x1 = 0;
player.x2 = 40;
}
if(player.x2 >= WINDOW_WIDTH)
{
player.x1 = WINDOW_WIDTH - 40;
player.x2 = WINDOW_WIDTH;
}
if(player.y1 <= 0)
{
player.y1 = 0;
player.y2 = player.y1 + 40;
}
if(player.y2 >= WINDOW_HEIGHT)
{
player.y1 = WINDOW_HEIGHT - 40;
player.y2 = WINDOW_HEIGHT;
}
}
if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
done = true;
}
if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = true;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = true;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = true;
break;
case ALLEGRO_KEY_UP:
keys[UP] = true;
break;
}
}
if(ev.type == ALLEGRO_EVENT_KEY_UP)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = false;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = false;
break;
case ALLEGRO_KEY_UP:
keys[UP] = false;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = false;
break;
}
}
if(redraw && al_is_event_queue_empty(event_queue))
{
al_draw_filled_rectangle(player.x1,player.y1,player.x2,player.y2,al_map_rgb(0,0,127));
al_draw_filled_rectangle(lover.x1,lover.y1,lover.x2,lover.y2,al_map_rgb(127,0,0));
al_flip_display();
al_clear_to_color(al_map_rgb(255,255,255));
}
}
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
return 0;
}
Can anybody help me out here? "Globals.h" is just a small header file which contains a struct for Character defining their x1, x2, y1, and y2 variables. Thanks.
As your code currently is, your redraw is not being controlled by your timer. Your redraw variable is being initialized to true, but it makes more sense to set it to false, and let your timer set it to true when appropriate. When your timer event fires, set redraw to true.
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue,&ev);
if(ev.timer.source == timer)
{
redraw = true;
}
Then when you check for redraw, set it back to false.
if(redraw && al_is_event_queue_empty(event_queue))
{
redraw = false;
al_draw_filled_rectangle(player.x1,player.y1,player.x2,player.y2,al_map_rgb(0,0,127));
al_draw_filled_rectangle(lover.x1,lover.y1,lover.x2,lover.y2,al_map_rgb(127,0,0));
al_flip_display();
al_clear_to_color(al_map_rgb(255,255,255));
}
Related
I started writing a little game but something with the image is not working. They're working fine at the beginning but after some moments there become black and then they disappear.
Don't be worried about the long code the relevant things with the images happen mostly in the following methods: build_mode_draw() and play_mode_draw().
I tested the program with a blue cube instead of a image and it worked fine
Probably I don't really understand how the image gets loaded
#include <SDL2/SDL.h>
#define WindowWidth 1500
#define WindowHight 800
#define ArrayGrosseBuildMode 100
#define StartFensterBlockmenge 10 // in Plockgröße bezüglich der kleineren Achse
bool end = false;
bool programm_part_run = true;
unsigned int play_mode_speed = 0;
unsigned int counter;
unsigned int counter_2;
unsigned int counter_3;
unsigned int blocksize;
unsigned int blocks_fit_in_X;
unsigned int blocks_fit_in_Y;
unsigned int play_mode_blockamount;
//unsigned int blockamount = 0;
bool build_mode_block_pos[ArrayGrosseBuildMode][ArrayGrosseBuildMode];
unsigned int play_mode_block_pos_X[WindowWidth]; // Fächer beschreiben
unsigned int play_mode_block_pos_Y[WindowHight]; // ||
//mouse variables
unsigned short int pressed_mouse_button = 0; // 0 = no , 1 = left , mouse Button pressed
unsigned int MouseX;
unsigned int MouseY;
//keyboard variables
//set window
SDL_Window* window = NULL;
//set renderer
SDL_Renderer* renderer = NULL;
//set event
SDL_Event event;
void input()
{
SDL_PollEvent(&event);
// reset variables
pressed_mouse_button = 0; // set to no mouse button pressed
switch(event.type)
{
case SDL_QUIT:
end = true;
programm_part_run = false;
break;
case SDL_MOUSEMOTION:
MouseX = event.motion.x;
MouseY = event.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
switch(event.button.button)
{
case SDL_BUTTON_LEFT:
pressed_mouse_button = 1;
break;
}
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_SPACE:
programm_part_run = false;
break;
}
}
}
void put_build_mode_grid_in_renderer()
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
for(counter = 0; counter <= blocks_fit_in_Y; counter = counter + 1)
{
SDL_RenderDrawLine(renderer,0,counter * blocksize,blocks_fit_in_X*blocksize,counter * blocksize);
}
for(counter = 0; counter <= blocks_fit_in_X; counter = counter + 1)
{
SDL_RenderDrawLine(renderer,counter * blocksize,0,counter * blocksize,blocks_fit_in_Y*blocksize);
}
}
void build_mode_draw()
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
SDL_RenderClear(renderer);
put_build_mode_grid_in_renderer();
SDL_Surface * image = SDL_LoadBMP("stealcube.bmp");
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer,image);
SDL_FreeSurface(image);
for(counter = 0; counter <= blocks_fit_in_X; counter = counter + 1)
{
for(counter_2 = 0; counter_2 <= blocks_fit_in_Y; counter_2 = counter_2 + 1)
{
if(build_mode_block_pos[counter][counter_2] == true)
{
SDL_Rect dstrect = { counter * blocksize, counter_2 * blocksize, blocksize, blocksize};
SDL_RenderCopy(renderer, texture, NULL, &dstrect);
}
}
}
SDL_RenderPresent(renderer);
}
void build_mode()
{
while(programm_part_run)
{
input();
if(pressed_mouse_button == 1)
{
build_mode_block_pos[MouseX/blocksize][MouseY/blocksize] = true;
}
build_mode_draw();
}
}
void play_mode_draw()
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
SDL_RenderClear(renderer);
SDL_Surface * image = SDL_LoadBMP("stealcube.bmp");
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer,image);
SDL_FreeSurface(image);
for(counter = 0; counter < play_mode_blockamount; counter = counter + 1)
{
SDL_Rect dstrect = { play_mode_block_pos_X[counter], play_mode_block_pos_Y[counter], blocksize, blocksize};
SDL_RenderCopy(renderer, texture, NULL, &dstrect);
}
SDL_RenderPresent(renderer);
}
void play_mode()
{
counter_3 = 0;
for(counter = 0; counter <= blocks_fit_in_X; counter = counter + 1)
{
for(counter_2 = 0; counter_2 <= blocks_fit_in_Y; counter_2 = counter_2 + 1)
{
if(build_mode_block_pos[counter][counter_2] == true)
{
play_mode_block_pos_X[counter_3] = counter*blocksize;
play_mode_block_pos_Y[counter_3] = counter_2*blocksize;
counter_3 = counter_3 + 1;
}
}
}
play_mode_blockamount = counter_3;
while(programm_part_run)
{
for(counter = 0; counter < play_mode_speed; counter = counter + 1)
{
input();
SDL_Delay(1);
}
for(counter = 0; counter <= play_mode_blockamount; counter = counter + 1)
{
if(play_mode_block_pos_Y[counter] < blocks_fit_in_Y * blocksize - blocksize)
{
play_mode_block_pos_Y[counter] = play_mode_block_pos_Y[counter] + 1;
}
}
play_mode_draw();
}
}
int main (int argc, char** argv)
{
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow
(
"Test Fenster :)", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WindowWidth,
WindowHight,
SDL_WINDOW_SHOWN
);
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
//setup
if(WindowWidth < WindowHight)
{
blocksize = WindowWidth/StartFensterBlockmenge;
}
else
{
blocksize = WindowHight/StartFensterBlockmenge;
}
blocks_fit_in_X = WindowWidth/blocksize;
blocks_fit_in_Y = WindowHight/blocksize;
while(!end)
{
programm_part_run = true;
build_mode();
programm_part_run = true;
play_mode();
}
}
I am attempting to make a simple scrolling shooter game with SDL2. I have a moving player on a screen, and I am trying to make the player shoot a bullet using an array (so they can shoot multiple bullets) however, when I press the space bar, nothing happens, and instead the bullet image sort of flashes in the top left corner.
Heres the same code in codepad: http://codepad.org/rOhE1AqY
#include <SDL.h>
#include <stdio.h> //use for things like printf, same as cout
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
//screend dimensions& sprtie dimensions
const int SCREEN_HEIGHT = 600;
const int SCREEN_WIDTH = 400;
const int SPRITE_WIDTH = 60;
const int SPRITE_HEIGHT = 80;
const int MAX_BULLETS = 50;
SDL_Window* Window = NULL;//the window rendering to
SDL_Surface* ScreenSurface = NULL;//surface contained by window
SDL_Surface* Background = NULL;
SDL_Surface* Player = NULL;
SDL_Surface* Enemy = NULL;
SDL_Surface* Bullet = NULL;
SDL_Surface* newBullet = NULL;
SDL_Rect posPlayer, posEnemy, posBullet, posnewBullet;
const Uint8* keystate = SDL_GetKeyboardState(NULL);
SDL_Event event;
class thePlayer
{
public:
thePlayer();
void player_movement();
void show_player();
private:
};
class theBullet
{
public:
theBullet();
bool isActive;
int x_position;
int y_position;
void bullet_movement();
void add_new_bullet();
void show_bullet();
private:
};
theBullet arrayofBullets[MAX_BULLETS];
class theEnemy
{
public:
theEnemy();
void enemy_movement();
void show_enemy();
private:
};
thePlayer::thePlayer()
{
posPlayer.x = 170;
posPlayer.y = SCREEN_HEIGHT;
posPlayer.w = 20;
posPlayer.h = 30;
}
void thePlayer::player_movement()
{
if(keystate[SDL_SCANCODE_LEFT])
{
posPlayer.x -= 2;
}
if(keystate[SDL_SCANCODE_RIGHT])
{
posPlayer.x += 2;
}
if(keystate[SDL_SCANCODE_UP])
{
posPlayer.y -= 2;
}
if(keystate[SDL_SCANCODE_DOWN])
{
posPlayer.y += 2;
}
if ((posPlayer.x + SPRITE_WIDTH) > SCREEN_WIDTH)
{
posPlayer.x = (SCREEN_WIDTH - SPRITE_WIDTH);
}
if ((posPlayer.y + SPRITE_HEIGHT) > SCREEN_HEIGHT)
{
posPlayer.y = (SCREEN_HEIGHT - SPRITE_HEIGHT);
}
}
void thePlayer::show_player()
{
SDL_BlitSurface(Player, NULL, ScreenSurface, &posPlayer);
SDL_SetColorKey(Player, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255));
}
theBullet::theBullet()
{
/*posBullet.x;
posBullet.y;
posBullet.w = 10;
posBullet.h = 15;*/
}
void theBullet::bullet_movement()
{
/*if(keystate[SDL_SCANCODE_SPACE])
{
posBullet.x = posPlayer.x + 25;
posBullet.y = posPlayer.y + 10;
}
posBullet.y -= 2;
if(posBullet.y < 0)
{
posBullet.y = -50;
}*/
}
void theBullet::show_bullet()
{
//SDL_BlitSurface(Bullet, NULL, ScreenSurface, &posBullet);
//SDL_SetColorKey(Bullet, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255));//removes white background
}
theEnemy::theEnemy()
{
srand (time(NULL));
posEnemy.x = rand() % 300 + 50;
posEnemy.y =0;
posEnemy.w = 35;
posEnemy.h = 60;
}
void theEnemy::enemy_movement()
{
posEnemy.y += 1;
if(posEnemy.y > SCREEN_HEIGHT)
{
posEnemy.y = SCREEN_HEIGHT +50;
}
}
void theEnemy::show_enemy()
{
SDL_BlitSurface(Enemy, NULL, ScreenSurface, &posEnemy);
SDL_SetColorKey(Enemy, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255));
}
bool initialise()
{
bool success = true;
if (SDL_Init(SDL_INIT_EVERYTHING) !=0)
{
cout<<"SDL_Init Error."<<SDL_GetError()<<endl;
success = false;
}
else
{
//create the window for game
Window = SDL_CreateWindow("Scrolling Shooter Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (Window == NULL)
{
cout<<"Window Error"<<SDL_GetError()<<endl;
success = false;
}
else
{
//get window surface
ScreenSurface = SDL_GetWindowSurface(Window);
}
}
return success;
}
bool LoadingMedia()
{
bool success = true;
Background = SDL_LoadBMP("background.bmp");
if (Background == NULL)
{
cout<<"Error in loading background."<<SDL_GetError()<<endl;
success = false;
}
Player = SDL_LoadBMP("spaceship.bmp");
if (Player == NULL)
{
cout<<"Error in loading player."<<SDL_GetError()<<endl;
success = false;
}
Enemy = SDL_LoadBMP("enemy.bmp");
if (Enemy == NULL)
{
cout<<"Error in loading enemy."<<SDL_GetError()<<endl;
success = false;
}
Bullet = SDL_LoadBMP("bullet.bmp");
if (Bullet == NULL)
{
cout<<"Error in loading bullet."<<SDL_GetError()<<endl;
success = false;
}
return success;
}
void closedown()
{
SDL_FreeSurface(Background);
Background = NULL;
SDL_FreeSurface(Player);
Player = NULL;
SDL_FreeSurface(Enemy);
Enemy = NULL;
SDL_DestroyWindow(Window);
Window = NULL;
SDL_Quit();
}
int main(int argc, char** argv)
{
bool quit = false;
thePlayer myPlayer;
theEnemy myEnemy;
theBullet myBullet;
if (!initialise())
{
cout<<"Failed to initialise"<<SDL_GetError()<<endl;
}
else
{
if (!LoadingMedia())
{
cout<<"Error loading media"<<SDL_GetError()<<endl;
}
}
//makes all bullets false
for (int i=0; i<MAX_BULLETS; i++)
{
arrayofBullets[i].isActive = false;
}
//GAME LOOP
while (quit == false)
{
SDL_BlitSurface(Background, NULL, ScreenSurface, NULL);
myPlayer.show_player();
myPlayer.player_movement();
while (SDL_PollEvent(&event))
{
if( event.type == SDL_QUIT )
{
quit = true;
break;
}
if(keystate[SDL_SCANCODE_SPACE])
{
for (int i=0; i<MAX_BULLETS; i++)
{
if (arrayofBullets[i].isActive == false)
{
arrayofBullets[i].x_position = posPlayer.x + 25;
arrayofBullets[i].y_position = posPlayer.y + 10;
arrayofBullets[i].isActive = true;
break;
}
}
}
//update game objects
for (int i=0; i<MAX_BULLETS; i++)
{
if (arrayofBullets[i].isActive == true)
{
arrayofBullets[i].y_position -= 2;
if (arrayofBullets[i].y_position < 0)
{
arrayofBullets[i].isActive = false;
}
}
}
for (int i=0; i<MAX_BULLETS; i++)
{
if (arrayofBullets[i].isActive == true)
{
SDL_BlitSurface(Bullet, NULL, ScreenSurface, NULL);
}
}
}
//myPlayer.show_player();
//myBullet.show_bullet();
//myEnemy.show_enemy();
//myPlayer.player_movement();
//myBullet.bullet_movement();
//myEnemy.enemy_movement();
SDL_UpdateWindowSurface(Window); //updates screen
}
closedown();
return 0;
}
SDL_BlitSurface(Bullet, NULL, ScreenSurface, NULL);
You haven't specified destination rect, so it will blit on left-top corner.
It should be
SDL_Rect dstrect;
dstrect.x = arrayofBullets[i].x_position;
dstrect.y = arrayofBullets[i].y_position;
dstrect.w = Bullet->w;
dstrect.h = Bullet->h;
SDL_BlitSurface(Bullet, NULL, ScreenSurface, &dstrect);
I have just recently started working with allegro 5 and decided to start with a basic pong game. I found one and decided to add some extras to it. After adding two more paddles and the needed variables and controls, I copied and pasted the collision code from the original two and added the needed variables. However, if I move the new paddles from the originals, they no longer bounce off the ball. The code is huge I know, but I have no idea whats causing the problem.
`
#include <stdio.h>
#include <cstdlib>
#include <sstream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_color.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
/* constants and definitions */
const int SCREEN_W = 960;
const int SCREEN_H = 720;
const float FPS = 60;
const int paddle_height = 96;
const int paddle_width = 16;
const int bouncer_size = 16;
enum MYKEYS{
KEY_UP,KEY_DOWN,KEY_W,KEY_S,KEY_E,KEY_D,KEY_O,KEY_L
};
/* functions */
void draw_paddle(float x,float y){
// fill
al_draw_filled_rounded_rectangle(x,y,x+paddle_width,y+paddle_height,3,3,al_color_html("729fcf"));
// outline
al_draw_rounded_rectangle(x,y,x+paddle_width,y+paddle_height,3,3,al_color_html("b5edff"),1.0);
// shine
al_draw_filled_rounded_rectangle(x,y,x+paddle_width/2,y+paddle_height-10,3,3,al_color_html("8abbef"));
}
void draw_ball(float x,float y){
// fill
al_draw_filled_circle(x,y,bouncer_size,al_color_html("6be97d"));
// shadow
al_draw_filled_circle(x+bouncer_size/4,y+bouncer_size/4,bouncer_size/3*2,al_color_html("59ce76"));
// shine
al_draw_filled_circle(x-bouncer_size/3,y-bouncer_size/3,bouncer_size/4,al_color_html("9bffaa"));
}
void draw_score(int l,int r){
std::ostringstream ostr;
ostr << l;
std::ostringstream ostr1;
ostr1 << r;
ALLEGRO_FONT *font = al_load_font("arial.ttf",72,0);
al_draw_filled_rectangle(SCREEN_W/2-5,SCREEN_H,SCREEN_W/2+5,-SCREEN_H,al_color_html("ffffff"));
al_draw_text(font,al_color_html("ffffff"),SCREEN_W/2-50,0,ALLEGRO_ALIGN_CENTRE,ostr.str().c_str());
al_draw_text(font,al_color_html("ffffff"),SCREEN_W/2+50,0,ALLEGRO_ALIGN_CENTER,ostr1.str().c_str());
al_destroy_font(font);
}
int main(){
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_BITMAP *right_paddle = NULL;
ALLEGRO_BITMAP *left_paddle = NULL;
ALLEGRO_BITMAP *right_sub_paddle = NULL;
ALLEGRO_BITMAP *left_sub_paddle = NULL;
ALLEGRO_BITMAP *bouncer = NULL;
float right_paddle_x =(SCREEN_W*4/5)-(paddle_width/2.0);
float right_paddle_y =(SCREEN_H/2.0)-(paddle_height/2.0);
float left_paddle_x =(SCREEN_W*1/5)-(paddle_width/2.0);
float left_paddle_y =(SCREEN_H/2.0)-(paddle_height/2.0);
float right_sub_paddle_x = (SCREEN_W*4/5)-(paddle_width/2.0)-100;
float right_sub_paddle_y =(SCREEN_H/2.0)-(paddle_height/2.0);
float left_sub_paddle_x = (SCREEN_W*1/5)-(paddle_width/2.0)+100;
float left_sub_paddle_y = (SCREEN_H/2.0)-(paddle_height/2.0);
float bouncer_x = SCREEN_W/2.0-bouncer_size/2.0;
float bouncer_y = SCREEN_H/2.0-bouncer_size/2.0;
float bouncer_dx = 8.0,bouncer_dy = -8.0;
float paddle_speed = 8.0;
bool key[8] = {false,false,false,false,false,false,false,false};
bool redraw = true;
bool doexit = false;
bool pause = false;
int left_score = 0;
int right_score = 0;
if(!al_init()){
fprintf(stderr,"failed to initialized allegro\n");
return -1;
}
if(!al_install_keyboard()){
fprintf(stderr,"failed to install keyboard\n");
return -1;
}
al_init_primitives_addon();
al_init_font_addon();
al_init_ttf_addon();
al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS,1,ALLEGRO_SUGGEST);
al_set_new_display_option(ALLEGRO_SAMPLES,4,ALLEGRO_SUGGEST);
//initialize display(w,h)
display = al_create_display(SCREEN_W,SCREEN_H);
if(!display){
fprintf(stderr,"failed to create display\n");
return -1;
}
timer = al_create_timer(1.0/FPS);
if(!timer){
fprintf(stderr,"failed to create timer\n");
return -1;
}
right_paddle = al_create_bitmap(paddle_width,paddle_height);
if(!right_paddle){
fprintf(stderr,"failed to create right paddle bitmap\n");
return -1;
}
left_paddle = al_create_bitmap(paddle_width,paddle_height);
if(!left_paddle){
fprintf(stderr,"failed to create left paddle bitmap\n");
return -1;
}
right_sub_paddle = al_create_bitmap(paddle_width,paddle_height);
if(!right_sub_paddle){
fprintf(stderr,"failed to create right sub paddle bitmap\n");
return -1;
}
left_sub_paddle = al_create_bitmap(paddle_width,paddle_height);
if(!left_sub_paddle){
fprintf(stderr,"failed to create left sub paddle bitmap\n");
return -1;
}
bouncer = al_create_bitmap(bouncer_size,bouncer_size);
if(!bouncer){
fprintf(stderr,"failed to create bouncer bitmap\n");
return -1;
}
al_set_target_bitmap(left_paddle);
al_clear_to_color(al_map_rgb(0,255,0));
al_set_target_bitmap(right_paddle);
al_clear_to_color(al_map_rgb(0,255,0));
al_set_target_bitmap(left_sub_paddle);
al_clear_to_color(al_map_rgb(0,255,0));
al_set_target_bitmap(right_sub_paddle);
al_clear_to_color(al_map_rgb(0,255,0));
al_set_target_bitmap(bouncer);
al_clear_to_color(al_map_rgb(0,0,255));
al_set_target_bitmap(al_get_backbuffer(display));
event_queue = al_create_event_queue();
if(!event_queue){
fprintf(stderr,"failed to create event queue\n");
return -1;
}
al_register_event_source(event_queue,al_get_display_event_source(display));
al_register_event_source(event_queue,al_get_timer_event_source(timer));
al_register_event_source(event_queue,al_get_keyboard_event_source());
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_start_timer(timer);
while(!doexit){
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue,&ev);
if(ev.type == ALLEGRO_EVENT_TIMER && !pause){
//logic for moving the paddles on input
if(key[KEY_UP] && right_paddle_y >= 4.0){
right_paddle_y -= paddle_speed;
}
if(key[KEY_DOWN] && right_paddle_y <= SCREEN_H-paddle_height-4.0){
right_paddle_y += paddle_speed;
}
if(key[KEY_W] && left_paddle_y >= 4.0){
left_paddle_y -= paddle_speed;
}
if(key[KEY_S] && left_paddle_y <= SCREEN_H-paddle_height-4.0){
left_paddle_y += paddle_speed;
}
if(key[KEY_E] && left_sub_paddle_y >= 4.0){
left_sub_paddle_y -= paddle_speed;
}
if(key[KEY_D] && left_sub_paddle_y <= SCREEN_H-paddle_height-4.0){
left_sub_paddle_y += paddle_speed;
}
if(key[KEY_O] && right_sub_paddle_y >= 4.0){
right_sub_paddle_y -= paddle_speed;
}
if(key[KEY_L] && right_sub_paddle_y <= SCREEN_H-paddle_height-4.0){
right_sub_paddle_y += paddle_speed;
}
//logic for the bouncer
if(bouncer_x < 0 || bouncer_x > SCREEN_W-bouncer_size){
if(bouncer_x < 0){
left_score += 1;
}else if(bouncer_x > SCREEN_W-bouncer_size){
right_score += 1;
}
paddle_speed = 8.0;
bouncer_x = SCREEN_W/2.0-bouncer_size/2.0;
bouncer_y = SCREEN_H/2.0-bouncer_size/2.0;
bouncer_dx = -bouncer_dx;
bouncer_dy = -bouncer_dx;
}
if(bouncer_y < 0 || bouncer_y > SCREEN_H-bouncer_size){
bouncer_dy = -bouncer_dy;
}
if(bouncer_x == left_paddle_x+(paddle_width/1.0)){
if(bouncer_y >= left_paddle_y+paddle_height || bouncer_y+bouncer_size < left_paddle_y){
}else{
paddle_speed += 1;
bouncer_dx = -bouncer_dx;
}
}
if(bouncer_x == right_paddle_x-(paddle_width/2.0)){
if(bouncer_y >= right_paddle_y+paddle_height || bouncer_y+bouncer_size < right_paddle_y){
}else{
paddle_speed += 1;
bouncer_dx = -bouncer_dx;
}
}
if(bouncer_x == left_sub_paddle_x+(paddle_width/1.0)){
if(bouncer_y >= left_sub_paddle_y+paddle_height || bouncer_y+bouncer_size < left_sub_paddle_y){
}else{
paddle_speed +=1;
bouncer_dx = -bouncer_dx;
}
}
if(bouncer_x == right_sub_paddle_x+(paddle_width/1.0)){
if(bouncer_y >= right_sub_paddle_y+paddle_height || bouncer_y+bouncer_size < right_sub_paddle_y){
}else{
paddle_speed +=1;
bouncer_dx = -bouncer_dx;
}
}
bouncer_x += bouncer_dx;
bouncer_y += bouncer_dy;
redraw = true;
}else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
break;
}else if(ev.type == ALLEGRO_EVENT_KEY_DOWN){
switch(ev.keyboard.keycode){
case ALLEGRO_KEY_UP:
key[KEY_UP] = true;
break;
case ALLEGRO_KEY_DOWN:
key[KEY_DOWN] = true;
break;
case ALLEGRO_KEY_W:
key[KEY_W] = true;
break;
case ALLEGRO_KEY_S:
key[KEY_S] = true;
break;
case ALLEGRO_KEY_E:
key[KEY_E] = true;
break;
case ALLEGRO_KEY_D:
key[KEY_D] = true;
break;
case ALLEGRO_KEY_O:
key[KEY_O] = true;
break;
case ALLEGRO_KEY_L:
key[KEY_L] = true;
break;
}
}else if(ev.type == ALLEGRO_EVENT_KEY_UP){
switch(ev.keyboard.keycode){
case ALLEGRO_KEY_UP:
key[KEY_UP] = false;
break;
case ALLEGRO_KEY_DOWN:
key[KEY_DOWN] = false;
break;
case ALLEGRO_KEY_W:
key[KEY_W] = false;
break;
case ALLEGRO_KEY_S:
key[KEY_S] = false;
break;
case ALLEGRO_KEY_E:
key[KEY_E] = false;
break;
case ALLEGRO_KEY_D:
key[KEY_D] = false;
break;
case ALLEGRO_KEY_O:
key[KEY_O] = false;
break;
case ALLEGRO_KEY_L:
key[KEY_L] = false;
break;
case ALLEGRO_KEY_ESCAPE:
doexit = true;
break;
case ALLEGRO_KEY_P:
if(pause){
pause = false;
}else{
pause = true;
}
break;
}
}
if(redraw && al_is_event_queue_empty(event_queue)){
redraw = false;
al_clear_to_color(al_map_rgb(0,0,0));
draw_score(right_score,left_score);
draw_paddle(right_sub_paddle_x,right_sub_paddle_y);
draw_paddle(left_sub_paddle_x,left_sub_paddle_y);
draw_paddle(right_paddle_x,right_paddle_y);
draw_paddle(left_paddle_x,left_paddle_y);
draw_ball(bouncer_x,bouncer_y);
//al_draw_bitmap(right_paddle,right_paddle_x,right_paddle_y,0);
//al_draw_bitmap(left_paddle,left_paddle_x,left_paddle_y,0);
//al_draw_bitmap(right_sub_paddle,right_sub_paddle_x,right_sub_paddle_y,0);
//al_draw_bitmap(left_sub_paddle,left_sub_paddle_x,left_sub_paddle_y,0);
//al_draw_bitmap(bouncer,bouncer_x,bouncer_y,0);
al_flip_display();
}
}
al_destroy_bitmap(bouncer);
al_destroy_bitmap(right_paddle);
al_destroy_bitmap(left_paddle);
al_destroy_bitmap(right_sub_paddle);
al_destroy_bitmap(left_sub_paddle);
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
return 0;
}`
Any help is greatly appreciated.
I'm pretty sure this is your problem:
float bouncer_dx = 8.0,bouncer_dy = -8.0;
^ notice it's always moving at 8px per game tick...
Further down in the code, you are checking if bouncer_x (and bouncer_y) is landing on a pixel that is exactly divisible by 8, and while it seems to work for the two original paddles, i'm willing to bet the bouncer_x value never equals the value to trigger this 'if' statement:
if(bouncer_x == left_sub_paddle_x+(paddle_width/1.0)) { ... }
because your new sub paddles have different width and math (divided by 1.0 and not 2.0)
Check it and see... this is just from looking over the code for a minute so i dunno for sure :)
Good luck!
I've checked everywhere in my code and I could not find the reason why my program freezes every time I load it. The problem started happening after I included a new AI header, "schoolboy.h". I checked to make sure that attempting to blit an image wasn't the problem, and it wasn't. So, after some testing, I figured that the problem lied in "void schoolboy_action()" within "schoolboy.h". Here is the entire project's code. I spent an hour looking at that section and could not find a solution. I know that the is really long, and that there are some holes in it that I plan on filling, but please bear with me.
Main.cpp
#include "schoolboy.h"
#include "include_file.h"
int main(int argc,char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
variables();
while (quit == 1)
{
while (MARS.alive == 1)
{
SDL_WM_SetCaption("Mobile Anihilation Robot System", NULL);
applysurface(0,0,background,screen);
MARS_action();
MARS_bullet_action(1);
gunshot_explosion_action(1);
if (create_schoolboy_variable == 1)
{create_schoolboy(); create_schoolboy_variable = 0;}
else if (create_schoolboy_variable < 1)
{create_schoolboy_variable = rand() % 1;};
schoolboy_action(0,0,0);
applysurface(MARS.x-25, MARS.y-25, MARS_image, screen);
SDL_Flip(screen);
SDL_Delay(1);
};
while (MARS.alive == 0)
{
SDL_WM_SetCaption("Mobil Anihilation Robot System", NULL);
};
};
SDL_FreeSurface(screen);
SDL_Quit();
return 0;
};
include_file.h
#ifndef INCLUDE_FILE_H_INCLUDED
#define INCLUDE_FILE_H_INCLUDED
#include <map>
#include <cstdlib>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
/*structs*/
struct instance_struct
{
int gunshot_explosion;
int schoolboy_instance;
};
struct MARS_struct
{
int x;
int y;
int alive;
int bullet;
};
struct MARS_bullet_struct
{
int x;
int y;
int direction;
int exist;
int id;
bool operator<(const MARS_bullet_struct & n)const{return this->id<n.id;}
};
struct schoolboy_struct
{
int x;
int y;
int direction;
int id;
int exist;
int walk_delay;
int shoot_delay;
int walked;
SDL_Rect clip[3];
bool operator<(const schoolboy_struct&n)const{return this->id<n.id;}
};
struct gunshot_explosion_struct
{
int x;
int y;
int id;
int life;
int exist;
bool operator<(const gunshot_explosion_struct&n)const{return this->id<n.id;}
};
/*declaring structs*/
MARS_struct MARS;
instance_struct instance_body;
std::map<int, MARS_bullet_struct>MARS_bullet;
std::map<int, schoolboy_struct>schoolboy;
std::map<int, gunshot_explosion_struct>gunshot_explosion;
/*applysurface*/
void applysurface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect* clip = NULL)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source,clip,destination,&offset);
};
/*declaring global variables*/
int quit;
int create_schoolboy_variable;
SDL_Event event;
SDL_Rect clip[3];
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *gunshot_explosion_image = NULL;
SDL_Surface *MARS_image = NULL;
SDL_Surface *MARS_bullet_image = NULL;
SDL_Surface *schoolboy_image = NULL;
/*giving variables values*/
void variables()
{
quit = 1;
MARS.alive = 1;
MARS.x = 256;
MARS.y = 256;
create_schoolboy_variable = 0;
clip[0].x = 0;
clip[0].y = 0;
clip[0].w = 50;
clip[0].h = 50;
clip[1].x = 50;
clip[1].y = 50;
clip[1].w = 100;
clip[1].h = 100;
clip[2].x = 100;
clip[2].y = 100;
clip[2].w = 150;
clip[2].h = 150;
clip[3].x = 150;
clip[3].y = 150;
clip[3].w = 200;
clip[3].h = 200;
screen = SDL_SetVideoMode(512,512,32,SDL_SWSURFACE);
background = IMG_Load("images/background.png");
gunshot_explosion_image = IMG_Load("images/gunshot_explosion.png");
MARS_image = IMG_Load("images/MARS.png");
MARS_bullet_image = IMG_Load("images/MARS_bullet.png");
schoolboy_image = IMG_Load("images/schoolboy.png");
};
void gunshot_explosion_action(int instance)
{
while (instance <= instance_body.gunshot_explosion)
{
if (gunshot_explosion[instance].exist == 0)
{
gunshot_explosion[instance].life = gunshot_explosion[instance].life + 1;
if (gunshot_explosion[instance].life > 7)
{gunshot_explosion[instance].exist = 1;};
applysurface(gunshot_explosion[instance].x-6,gunshot_explosion[instance].y-6,gunshot_explosion_image,screen);
};
instance = instance + 1;
};
};
#endif // INCLUDE_FILE_H_INCLUDED
MARS.h
#ifndef MARS_H_INCLUDED
#define MARS_H_INCLUDED
#include "include_file.h"
/*character functions*/
void MARS_action()
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
MARS.alive = 2;
quit = 0;
};
if (event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_a:
if(MARS.x > 0){MARS.x = MARS.x - 16;}; break;
case SDLK_d:
if(MARS.x < 512){MARS.x = MARS.x + 16;}; break;
case SDLK_w:
if(MARS.y > 0){MARS.y = MARS.y - 16;}; break;
case SDLK_s:
if(MARS.y < 512){MARS.y = MARS.y + 16;}; break;
case SDLK_LEFT:
MARS.bullet = MARS.bullet + 1;
MARS_bullet[MARS.bullet].direction = 2;
MARS_bullet[MARS.bullet].x = MARS.x-25;
MARS_bullet[MARS.bullet].y = MARS.y;
instance_body.gunshot_explosion = instance_body.gunshot_explosion+1;
gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x-25;
gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y;
break;
case SDLK_RIGHT:
MARS.bullet = MARS.bullet + 1;
MARS_bullet[MARS.bullet].direction = 0;
MARS_bullet[MARS.bullet].x = MARS.x+25;
MARS_bullet[MARS.bullet].y = MARS.y;
instance_body.gunshot_explosion = instance_body.gunshot_explosion+1;
gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x+25;
gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y;
break;
case SDLK_UP:
MARS.bullet = MARS.bullet + 1;
MARS_bullet[MARS.bullet].direction = 1;
MARS_bullet[MARS.bullet].x = MARS.x;
MARS_bullet[MARS.bullet].y = MARS.y-25;
instance_body.gunshot_explosion = instance_body.gunshot_explosion+1;
gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x;
gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y-25;
break;
case SDLK_DOWN:
MARS.bullet = MARS.bullet + 1;
MARS_bullet[MARS.bullet].direction = 3;
MARS_bullet[MARS.bullet].x = MARS.x;
MARS_bullet[MARS.bullet].y = MARS.y+25;
instance_body.gunshot_explosion = instance_body.gunshot_explosion+1;
gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x;
gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y+25;
break;
case SDLK_ESCAPE: quit = 0; MARS.alive = 2; break;
};
};
};
};
void MARS_bullet_action(int instance)
{
while (instance <= MARS.bullet)
{
if (MARS_bullet[instance].exist == 0)
{
if (MARS_bullet[instance].direction == 0)
{MARS_bullet[instance].x = MARS_bullet[instance].x + 5;};
if (MARS_bullet[instance].direction == 1)
{MARS_bullet[instance].y = MARS_bullet[instance].y - 5;};
if (MARS_bullet[instance].direction == 2)
{MARS_bullet[instance].x = MARS_bullet[instance].x - 5;};
if (MARS_bullet[instance].direction == 3)
{MARS_bullet[instance].y = MARS_bullet[instance].y + 5;};
if (MARS_bullet[instance].x < 0 or MARS_bullet[instance].x > 512 or MARS_bullet[instance].y < 0 or MARS_bullet[instance].y > 512)
{MARS_bullet[instance].exist = 1;};
applysurface(MARS_bullet[instance].x-5, MARS_bullet[instance].y-5, MARS_bullet_image, screen);
};
instance = instance + 1;
};
};
#endif // MARS_H_INCLUDED
schoolboy.h
#ifndef SCHOOLBOY_H_INCLUDED
#define SCHOOLBOY_H_INCLUDED
#include "include_file.h"
void create_schoolboy(int positionx = 0, int positiony = 0)
{
instance_body.schoolboy_instance = instance_body.schoolboy_instance + 1;
positionx = rand() % 1;
positiony = rand() % 1;
if (positionx == 0 and positiony == 0)
{
schoolboy[instance_body.schoolboy_instance].x = 0;
schoolboy[instance_body.schoolboy_instance].y = 0;
};
if (positionx == 1 and positiony == 0)
{
schoolboy[instance_body.schoolboy_instance].x = 512;
schoolboy[instance_body.schoolboy_instance].y = 0;
};
if (positionx == 0 and positiony == 1)
{
schoolboy[instance_body.schoolboy_instance].x = 0;
schoolboy[instance_body.schoolboy_instance].y = 512;
};
if (positionx == 1 and positiony == 1)
{
schoolboy[instance_body.schoolboy_instance].x = 512;
schoolboy[instance_body.schoolboy_instance].y = 512;
};
};
void schoolboy_action(int instance, int bullet, int first_direction)
{
while (instance <= instance_body.schoolboy_instance)
{
first_direction = rand() % 1;
if (schoolboy[instance].exist == 0)
{
while (bullet <= MARS.bullet)
{
if (schoolboy[instance].x-12 >= MARS_bullet[bullet].x and MARS_bullet[bullet].x <= schoolboy[instance].x+12 and schoolboy[instance].y-12 >= MARS_bullet[bullet].y and MARS_bullet[bullet].y <= schoolboy[instance].y+12)
{
schoolboy[instance].exist = 1;
};
bullet = bullet + 1;
};
if (schoolboy[instance].walk_delay == 0)
{
if (first_direction == 0)
{
schoolboy[instance].walked = 0;
if (MARS.x > schoolboy[instance].x)
{
schoolboy[instance].x = schoolboy[instance].x + 16;
applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[0]);
schoolboy[instance].walked = 1;
}
else if (MARS.x < schoolboy[instance].x)
{
schoolboy[instance].x = schoolboy[instance].x - 16;
applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[2]);
schoolboy[instance].walked = 1;
};
if (schoolboy[instance].walked = 0)
{
if (MARS.y > schoolboy[instance].y)
{
schoolboy[instance].y = schoolboy[instance].y+16;
applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[3]);
schoolboy[instance].walked = 1;
}
else if (MARS.y < schoolboy[instance].y)
{
schoolboy[instance].y = schoolboy[instance].y+16;
applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[1]);
schoolboy[instance].walked = 1;
};
};
};
if (first_direction == 1)
{
schoolboy[instance].walked = 0;
if (MARS.y > schoolboy[instance].y)
{
schoolboy[instance].y = schoolboy[instance].y+16;
applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[3]);
schoolboy[instance].walked = 1;
}
else if (MARS.y < schoolboy[instance].y)
{
schoolboy[instance].y = schoolboy[instance].y+16;
applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[1]);
schoolboy[instance].walked = 1;
};
if (schoolboy[instance].walked = 0)
{
if (MARS.x > schoolboy[instance].x)
{
schoolboy[instance].x = schoolboy[instance].x + 16;
applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[0]);
schoolboy[instance].walked = 1;
}
else if (MARS.x < schoolboy[instance].x)
{
schoolboy[instance].x = schoolboy[instance].x - 16;
applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[2]);
schoolboy[instance].walked = 1;
};
};
};
schoolboy[instance].walk_delay = schoolboy[instance].walk_delay + 1;
}
else {schoolboy[instance].walk_delay = schoolboy[instance].walk_delay + 1;};
if (schoolboy[instance].walk_delay == 10){schoolboy[instance].walk_delay = 0;};
};
};
instance = instance + 1;
};
#endif // SCHOOLBOY_H_INCLUDED
All of my files are involved with each other, especially schoolboy.h, which is why I put them down. If you find the answer, can you explain why that's so. Any help appreciated!
Your instance = instance + 1 line at the end of schoolboy.h looks like it's outside the while loop that relies on it being incremented to eventually end.
I'm new in Allegro 5, I've written some code with the few tutorials there are so far, but I cannot get the joystick sticks input.
Here the code, it is just two bars moving perpendicularly(and a very lazy approach, just swapped the x an y coordinates on the second bar).
#pragma comment (lib,"allegro-5.0.2-monolith-md-debug.lib")
#include <stdio.h>
#include <string>
#include <iostream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#define FPS 0xFF
#define SCREEN_W 1000
#define SCREEN_H 750
#define BAR_W 75
#define BAR_H 10
enum KEYS
{
KEY_LEFT, KEY_RIGHT
};
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_JOYSTICK *joystick = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_BITMAP *bouncer = NULL;
float bouncer_x = SCREEN_W / 2.0 - BAR_W / 2.0;
float bouncer_y = SCREEN_H - BAR_H;
bool key[2] = {false, false};
bool redraw = true;
bool doexit = false;
if(!al_init())
{
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
if(!al_install_keyboard())
{
fprintf(stderr, "failed to initialize the keyboard!\n");
return -1;
}
if(!al_install_joystick())
{
fprintf(stderr, "failed to initialize the joystick!\n");
}
al_reconfigure_joysticks();
joystick=al_get_joystick(al_get_num_joysticks()-1);
timer = al_create_timer(1.0 / FPS);
if(!timer)
{
fprintf(stderr, "failed to create timer!\n");
return -1;
}
display = al_create_display(SCREEN_W, SCREEN_H);
if(!display)
{
fprintf(stderr, "failed to create display!\n");
al_destroy_timer(timer);
return -1;
}
bouncer = al_create_bitmap(BAR_W, BAR_H);
if(!bouncer)
{
fprintf(stderr, "failed to create bouncer bitmap!\n");
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}
al_set_target_bitmap(bouncer);
al_clear_to_color(al_map_rgb(0, 0, 255));
al_set_target_bitmap(al_get_backbuffer(display));
event_queue = al_create_event_queue();
if(!event_queue)
{
fprintf(stderr, "failed to create event_queue!\n");
al_destroy_bitmap(bouncer);
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_joystick_event_source());
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_start_timer(timer);
while(!doexit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_TIMER)
{
if(key[KEY_LEFT] && bouncer_x >= 2.0)
bouncer_x -= 2.0;
if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - BAR_W - 2.0)
bouncer_x += 2.0;
redraw = true;
}
else if(ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS
&& ev.joystick.stick == 0
&& ev.joystick.axis == 0)
{
float joypos=ev.joystick.pos;
if(joypos<0 && bouncer_x >= 2.0)
bouncer_x-=joypos;
if(joypos>0 && bouncer_x <= SCREEN_W - BAR_W - 2.0)
bouncer_x+=joypos;
if(joypos=0)
bouncer_x=SCREEN_W/2;
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
break;
else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_LEFT:
key[KEY_LEFT] = true;
break;
case ALLEGRO_KEY_RIGHT:
key[KEY_RIGHT] = true;
break;
}
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_LEFT:
key[KEY_LEFT] = false;
break;
case ALLEGRO_KEY_RIGHT:
key[KEY_RIGHT] = false;
break;
case ALLEGRO_KEY_ESCAPE:
doexit = true;
break;
}
}
if(redraw && al_is_event_queue_empty(event_queue))
{
std::string str=al_get_joystick_name(joystick);
redraw = false;
al_clear_to_color(al_map_rgb(0,0,0));
al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
al_draw_bitmap(bouncer, bouncer_y, bouncer_x, 0);
std::cout << ev.joystick.pos << " ";
std::cout << str << " ";
std::cout << al_get_joystick_active(joystick) << std::endl;
al_flip_display();
}
}
al_destroy_bitmap(bouncer);
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
return 0;
}
Now, the trouble: ev.joystick.pos has a relatively static value (viewed trough MSVC2010 debugger), no matter if I have all the axis of my joystick at the max position.
Also, I don't know how to get the value of an specific axis of an specific stick. I managed to get the bar move only when an specific axis and stick changes, but not "how much" it changes.
Thanks in advance.
I can't see what you're doing wrong, but I got the controller working in my game, so maybe my code will shed some insight. By the way, I'm using an Xbox 360 controller. Actually, I'm using a GameStop brand 360 controller, so it's not technically official. Anyway, here's my relevant code (the full source is a monster):
if(ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS){
if(ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS){
if(ev.joystick.stick == 0){ //float joys[3][2]
joys[ev.joystick.stick][ev.joystick.axis] = ev.joystick.pos;
}
}
void SetPosition(){
int leftStick = 0;
int rightStick = 1;
int dPad = 2;
int x = 0;
int y = 1;
int z = 2;
bitmapX += joys[leftStick][x];
bitmapY += joys[leftStick][y];
}