Getting joystick input with Allegro 5.0.2 - c++

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];
}

Related

Playing sine soundwave with SDL2 - noise / scratch issue

My goal is to create an SDL window plotting different waveforms and playing an indefinite sound of this wave. By pressing specific keys, the parameters of the wave, like the amplitude, frequency or waveform can be modified.
The problem is that even a simple sine wave which looks nice when plotted, sounds noisy. I don't understand why.
Code:
#include "Graph.h"
#include <thread>
#include <iostream>
#include <sstream>
#include <string>
int main(int argc, char* argv[]){
Graph* g = new Graph();
int i;
std::cin >> i;
return 0;
}
int graphThreadFunc(void *pointer){
Graph* grid = (Graph*)pointer;
grid->init();
return 0;
}
// SDL calls this function whenever it wants its buffer to be filled with samples
void SDLAudioCallback(void *data, Uint8 *buffer, int length){
uint8_t *stream = (uint8_t*)buffer;
Graph* graph = (Graph*)data;
for (int i = 0; i <= length; i++){
if (graph->voice.audioLength <= 0)
stream[i] = graph->getSpec()->silence; // 128 is silence in a uint8 stream
else
{
stream[i] = graph->voice.getSample();
graph->voice.audioPosition++;
// Fill the graphBuffer with the first 1000 bytes of the wave for plotting
if (graph->graphPointer < 999)
graph->graphBuffer[graph->graphPointer++] = stream[i];
}
}
}
Graph::Graph()
{
// spawn thread
SDL_Thread *refresh_thread = SDL_CreateThread(graphThreadFunc, NULL, this);
}
SDL_AudioSpec* Graph::getSpec(){
return &this->spec;
}
void Graph::init()
{
// Init SDL & SDL_ttf
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);
SDL_zero(desiredDeviceSpec);
desiredDeviceSpec.freq = 44100; // Sample Rate
desiredDeviceSpec.format = AUDIO_U8; // Unsigned 8-Bit Samples
desiredDeviceSpec.channels = 1; // Mono
desiredDeviceSpec.samples = 2048; // The size of the Audio Buffer (in number of samples, eg: 2048 * 1 Byte (AUDIO_U8)
desiredDeviceSpec.callback = SDLAudioCallback;
desiredDeviceSpec.userdata = this;
dev = SDL_OpenAudioDevice(NULL, 0, &desiredDeviceSpec, &spec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
if (dev == 0) {
printf("\nFailed to open audio: %s\n", SDL_GetError());
}
else {
SDL_PauseAudioDevice(dev, 1); /* pause! */
SDL_PauseAudio(1);
}
// Create an application window with the following settings:
window = SDL_CreateWindow(
WINDOW_TITLE.c_str(), // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
WINDOW_WIDTH, // width, in pixels
WINDOW_HEIGHT, // height, in pixels
SDL_WINDOW_SHOWN // flags - see below
);
// Check if the window was successfully created
if (window == NULL) {
// In case the window could not be created...
printf("Could not create window: %s\n", SDL_GetError());
return;
}
else{
voice.waveForm = Graph::Voice::WaveForm::SINE;
voice.amp = 120;
voice.frequency = 440;
SDL_PauseAudioDevice(dev, 1); // play
graphPointer = 0;
voice.audioLength = 44100;
voice.audioPosition = 0;
SDL_PauseAudioDevice(dev, 0); // play
SDL_Delay(200);
drawGraph();
mainLoop();
return;
}
}
void Graph::mainLoop()
{
while (thread_exit == 0){
SDL_Event event;
bool hasChanged = false;
while (SDL_PollEvent(&event)) {
switch (event.type)
{
case SDL_KEYDOWN:
{
hasChanged = true;
if (event.key.keysym.scancode == SDL_SCANCODE_SPACE){
//pause_thread = !pause_thread;
switch (voice.waveForm){
case Voice::SINE:
{
voice.waveForm = Graph::Voice::WaveForm::TRIANGLE;
break;
}
case Voice::TRIANGLE:
{
voice.waveForm = Graph::Voice::WaveForm::RECT;
break;
}
case Voice::RECT:
{
voice.waveForm = Graph::Voice::WaveForm::SAWTOOTH;
break;
}
case Voice::SAWTOOTH:
{
voice.waveForm = Graph::Voice::WaveForm::SINE;
break;
}
default:
break;
}
}
else if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE){
exit();
}
else if (event.key.keysym.scancode == SDL_SCANCODE_RETURN){
}
else if (event.key.keysym.scancode == SDL_SCANCODE_LEFT){
voice.frequency -= 2;
}
else if (event.key.keysym.scancode == SDL_SCANCODE_RIGHT){
voice.frequency += 2;
}
else if (event.key.keysym.scancode == SDL_SCANCODE_UP){
voice.amp += 2;
}
else if (event.key.keysym.scancode == SDL_SCANCODE_DOWN){
voice.amp -= 2;
}
else{
}
break;
}
case SDL_QUIT:
{
exit();
return;
break;
}
default: /* unhandled event */
break;
}
}
if (!pause_thread && hasChanged)
{
//SDL_PauseAudioDevice(dev, 1); // play
graphPointer = 0;
voice.audioLength = 44100;
voice.audioPosition = 0;
SDL_PauseAudioDevice(dev, 0); // play
SDL_Delay(200);
drawGraph();
}
//voice.waveForm = Voice::WaveForm::TRIANGLE;
//SDL_Delay(n); // delay the program to prevent the voice to be overridden before it has been played to the end
//SDL_PauseAudioDevice(dev, 1); // pause
SDL_Delay(REFRESH_INTERVAL);
//SDL_PauseAudioDevice(dev, 1); // pause
}
return;
}
void Graph::drawGraph()
{
SDL_Renderer *renderer = SDL_GetRenderer(window);
if (renderer == nullptr)
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED);
// Set background color
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
// Clear winow
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 22, 22, 22, 255);
for (int x = 0; x < WINDOW_WIDTH; x++){
uint8_t y = graphBuffer[x];
SDL_RenderDrawPoint(renderer, x, WINDOW_HEIGHT - y);
}
SDL_RenderPresent(renderer);
return;
}
void Graph::exit(){
thread_exit = 1;
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
}
uint8_t Graph::Voice::getSample(){
switch (waveForm){
case SINE:
{
float sineStep = 2 * M_PI * audioPosition * frequency / 44100;
return (amp * sin(sineStep)) + 128;
break;
}
case RECT:
break;
case SAWTOOTH:
break;
case TRIANGLE:
break;
default:
return 0;
}
}
And the header file:
#ifndef GRAPH_H
#define GRAPH_H
#include "SDL.h"
#include "SDL_audio.h"
#include <stdio.h>
#include <cmath>
#include <string>
#include <stack>
/* Constants */
const int REFRESH_INTERVAL = 50; // mseconds
const int WINDOW_WIDTH = 1000;
const int WINDOW_HEIGHT = 255;
const std::string WINDOW_TITLE = "Wave Graph";
class Graph
{
private:
SDL_Window *window; // Declare a pointer
// SDL audio stuff
SDL_AudioSpec desiredDeviceSpec;
SDL_AudioSpec spec;
SDL_AudioDeviceID dev;
int thread_exit = 0;
bool pause_thread = false;
public:
Graph();
void init();
void mainLoop();
void drawGraph();
void exit();
SDL_AudioSpec* getSpec();
struct Voice{
int frequency; // the frequency of the voice
int amp; // the amplitude of the voice
int audioLength; // number of samples to be played, eg: 1.2 seconds * 44100 samples per second
int audioPosition = 0; // counter
enum WaveForm{
SINE = 0, RECT = 1, SAWTOOTH = 2, TRIANGLE = 3
} waveForm;
uint8_t getSample();
} voice;
int graphPointer = 0;
uint8_t graphBuffer[1000];
};
#endif
Your SDLAudioCallback() is writing an extra byte off the end of buffer:
void SDLAudioCallback(void *data, Uint8 *buffer, int length)
{
...
for (int i = 0; i <= length; i++)
// ^^ huh?
{
...
}
}
Changing the <= to just < fixes the crackles on my system.
Generally C-style "byte pointer + length" APIs expect a left-closed, right-open interval: [0, length). I.e., you can access buffer[length - 1] but not buffer[length].

Main Menu for 2d game in ALLEGRO5

I've big problem with my university project.
I tried to make simple game in Allegro5, but I've stopped at making Main Menu.
I've basic code, but on 4 possibility: Start Game, Options, Credits,Exit. I can see just one of them ( exit ). Don't know how to fix it, can you help me ?
///////////// MENU
string menu[4] = { "Start Game", "Options", "Credits", "Exit" };
while (!quit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER)
{
redraw = true;
}
if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch (ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
checked--;
break;
case ALLEGRO_KEY_DOWN:
checked++;
break;
case ALLEGRO_KEY_ESCAPE:
quit = true;
break;
case ALLEGRO_KEY_ENTER:
active = true;
break;
}
}
if (checked < 0) //JAK NAM WYJEDZIE PONIZEJ ZERA TO USTAWIAMY NA 3 (ZAPETLA SIE MENU, JAKBYS CHCIAL ZABLOKOWAC TO DAJ NA 0)
{
checked = 3;
}
if (checked > 3)
{
checked = 0;
}
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
break;
}
**if (redraw && al_is_event_queue_empty(event_queue)) // JEZELI NIE WYKRYWA ZADNEGO RUCHU TO
{
redraw = false; // PRZERYSOWANIE WYNOSI 0
al_clear_to_color(al_map_rgb(0, 0, 0));
for (int i = 0; i < 4; i++)
{
int color;
if (i == checked)
{
color = 255;
}
else
{
color = 11;
}
al_draw_bitmap(mmenu, 0, 0, NULL);
al_draw_text(font, al_map_rgb(123, color, 45), 50, i * 60, NULL, menu[i].c_str()); // I PISZEMY TEKST
}
}
/*else if () // JEDNAK JAK ZOSTANIE WYKRYTY RUCH TO
{
redraw = true; // NASTĘPUJE PRZERYSOWANIE BITMAPY ZE ZMIENIONA OPCJA MENU
}*/**
al_flip_display();
}
al_destroy_display(display);
al_destroy_timer(timer);
al_destroy_bitmap(mmenu);
al_destroy_event_queue(event_queue);
return 0;
}

SDL2 'Bullet' movement not working?

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);

Allegro pong collision problems

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!

Weird visual error with drawing Allegro primitives

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));
}