Seg fault with Allegro bitmaps - c++

I am using liballegro4.2-dev on Ubuntu 14.04 to (hopefully) write a simple game. My code compiles fine using the command g++ Picker.cpp -o out ``allegro-config --libs, but when I run the code I always get the error message Shutting down Allegro due to signal #11 Segmentation fault (core dumped). I suspect this has something to do with the line: masked_blit(face, screen, 0, 0, 0, 0, 64, 64);, but Internet searches turn nothing up and I am out of ideas. The full code is below:
#include <allegro.h>
void setUpAllegro()
{
//INIT
allegro_init();
install_keyboard();
install_timer();
install_mouse();
install_sound( DIGI_AUTODETECT, MIDI_AUTODETECT, 0 );
set_color_depth( 16 );
bool fullscreen = false;
if (fullscreen == true)
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
else
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
//END INIT
}
class Player
{
public:
int x;
int y;
void Move(int dir)
{
switch(dir)
{
case 1:
y += 5;
break;
case 2:
y -= 5;
break;
case 3:
x += 5;
break;
case 4:
x -= 5;
break;
default:
//exit(1);
break;
}
}
};
int main()
{
setUpAllegro();
Player player;
bool done = false;
BITMAP *buffer = create_bitmap( 640, 480 );
BITMAP *face;
face = load_bitmap("/home/alexander/Desktop/Pooper Picker/fake.BMP",NULL);
while (!done)
{
if( key[KEY_ESC] )
done = true;
if( key[KEY_UP])
player.Move(1);
else if( key[KEY_DOWN] )
player.Move(2);
if ( key[KEY_LEFT] )
player.Move(3);
else if( key[KEY_RIGHT] )
player.Move(4);
masked_blit(face, screen, 0, 0, 0, 0, 64, 64);
blit( buffer, screen, 0, 0, 5, 5, 640, 480 );
release_screen();
clear_bitmap( buffer );
}
//free memory
destroy_bitmap( buffer );
return 0;
}
END_OF_MAIN();
Thanks for the help!

Related

SDL2 TTF text renders randomly disappearing

im using SDL2 and just grasped some of the basic-concepts of SDL TTF, but for some reason after a certain amount of time running the app, my text elements disappear with seemingly no reason why, can someone please tell me what i need to do to top my text renders from disappearing? what went wrong here?
#include <iostream>
#include <iomanip>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include "AdviLib.h"
#define SDLKCM SDLK_COMMA
#define SDLKPR SDLK_PERIOD
#define SDLKSP SDLK_SPACE
#define SDLKES SDLK_ESCAPE
void Screenshot(SDL_Renderer* r, const char* name, int w, int h)
{
const uint32_t format{ SDL_PIXELFORMAT_ARGB8888 };
SDL_Surface* sur{ SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, format) };
SDL_RenderReadPixels(r, NULL, format, sur->pixels, sur->pitch);
SDL_SaveBMP(sur, name);
SDL_FreeSurface(sur);
std::cout << name << " captured!\n";
}
int main(int args, char* argc[])
{
// before SDL init
std::cout << "enter the name of the session: "; string sesh{ sInput() };
std::cout << "enter the name of the local, non .bmp, image: "; string image{ sInput() };
// SDL init
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
TTF_Init();
const int W{ 800 }; const int H{ 750 };
SDL_Window* win{ SDL_CreateWindow(sesh.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, W, H, SDL_WINDOW_SHOWN) };
SDL_Renderer* ren{ SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC) };
SDL_Surface* img{ IMG_Load(image.c_str()) };
TTF_Font* font{ TTF_OpenFont("CRYSRG__.ttf", 40) };
SDL_Color color{ 205, 99, 35 };
SDL_Texture* tex{ SDL_CreateTextureFromSurface(ren, img) };
SDL_Event e;
// buffer variables
string Txt; string Tx2; string Tx3; string Tx4; string Tx5; string Tx6; string Tx7;
SDL_Surface* ts1 = nullptr; SDL_Surface* ts2 = nullptr; SDL_Surface* ts3 = nullptr; SDL_Surface* ts4 = nullptr; SDL_Surface* ts5 = nullptr; SDL_Surface* ts6 = nullptr; SDL_Surface* ts7 = nullptr;
SDL_Texture* tb1 = nullptr; SDL_Texture* tb2 = nullptr; SDL_Texture* tb3 = nullptr; SDL_Texture* tb4 = nullptr; SDL_Texture* tb5 = nullptr; SDL_Texture* tb6 = nullptr; SDL_Texture* tb7 = nullptr;
int w{ img->w }; int h{ img->h }; int x{ randomWithinRange(W - w) }; int y{ randomWithinRange(H - h) };
int tbW{ 0 }; int tbH{ 0 }; int X{ 2 }; int tbY{ 1 }; int tb2Y{ 32 }; int tb3Y{ 63 }; int tb4Y{ 94 }; int tb5Y{ 125 }; int tb6Y{ 156 }; int tb7Y{ 187 };
int ms{ 1 };
bool q{ false };
double ANG{ 0 }; int ANGI{ 0 };
// before main loop
// main loop
while (!q)
{
// before buffer setup
SDL_Rect IR{ x, y, w, h };
// buffer setup
ANGI = ANG;
int _TXT_RED = color.r; int _TXT_GRN = color.g; int _TXT_BLU = color.b;
//current x pos, current y pos, current angle, current speed, text color red, text color grn, text color blue,
char buf1[255]; char buf2[255]; char buf3[255]; char buf4[255]; char buf5[255]; char buf6[255]; char buf7[255];
sprintf_s(buf1, "x: %i", x); sprintf_s(buf2, "y: %i", y); sprintf_s(buf3, "a: %i", ANGI); sprintf_s(buf4, "MS: %i", ms); sprintf_s(buf5, "TR: %i", _TXT_RED); sprintf_s(buf6, "TG: %i", _TXT_GRN);
sprintf_s(buf7, "TB: %i", _TXT_BLU);
Txt = buf1; Tx2 = buf2; Tx3 = buf3; Tx4 = buf4; Tx5 = buf5; Tx6 = buf6; Tx7 = buf7;
ts1 = TTF_RenderText_Blended(font, Txt.c_str(), color); ts2 = TTF_RenderText_Blended(font, Tx2.c_str(), color); ts3 = TTF_RenderText_Blended(font, Tx3.c_str(), color);
ts4 = TTF_RenderText_Blended(font, Tx4.c_str(), color); ts5 = TTF_RenderText_Blended(font, Tx5.c_str(), color); ts6 = TTF_RenderText_Blended(font, Tx6.c_str(), color);
ts7 = TTF_RenderText_Blended(font, Tx7.c_str(), color);
tb1 = SDL_CreateTextureFromSurface(ren, ts1); tb2 = SDL_CreateTextureFromSurface(ren, ts2); tb3 = SDL_CreateTextureFromSurface(ren, ts3); tb4 = SDL_CreateTextureFromSurface(ren, ts4);
tb5 = SDL_CreateTextureFromSurface(ren, ts5); tb6 = SDL_CreateTextureFromSurface(ren, ts6); tb7 = SDL_CreateTextureFromSurface(ren, ts7);
SDL_QueryTexture(tb1, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb2, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb3, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb4, NULL, NULL, &tbW, &tbH);
SDL_QueryTexture(tb5, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb6, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb7, NULL, NULL, &tbW, &tbH);
SDL_Rect Rct{ X, tbY, tbW, tbH }; SDL_Rect Rc2{ X, tb2Y, tbW, tbH }; SDL_Rect Rc3{ X, tb3Y, tbW, tbH }; SDL_Rect Rc4{ X, tb4Y, tbW, tbH }; SDL_Rect Rc5{ X, tb5Y, tbW, tbH }; SDL_Rect Rc6{ X, tb6Y, tbW, tbH };
SDL_Rect Rc7{ X, tb7Y, tbW, tbH };
// set display renders
SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
SDL_RenderClear(ren);
SDL_RenderCopyEx(ren, tex, NULL, &IR, ANG, 0, SDL_FLIP_NONE);
SDL_RenderCopy(ren, tb1, NULL, &Rct); SDL_RenderCopy(ren, tb2, NULL, &Rc2); SDL_RenderCopy(ren, tb3, NULL, &Rc3); SDL_RenderCopy(ren, tb4, NULL, &Rc4); SDL_RenderCopy(ren, tb5, NULL, &Rc5);
SDL_RenderCopy(ren, tb6, NULL, &Rc6); SDL_RenderCopy(ren, tb7, NULL, &Rc7);
SDL_RenderPresent(ren);
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT) q = true;
else if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_w: y -= ms; break; case SDLK_s: y += ms; break;
case SDLK_a: x -= ms; break; case SDLK_d: x += ms; break;
case SDLK_r: if (color.r < 255) color.r += 1; else if (color.r == 255) color.r = 0; break;
case SDLK_g: if (color.g < 255) color.g += 1; else if (color.g == 255) color.g = 0; break;
case SDLK_b: if (color.b < 255) color.b += 1; else if (color.b == 255) color.b = 0; break;
case SDLKCM: ANG -= 1; break;
case SDLKPR: ANG += 1; break;
case SDLKSP: Screenshot(ren, "capture.bmp", W, H); break;
case SDLK_q: if (ms > 1) { ms -= 1; /*std::cout << "ms decreased to " << ms << "\n";*/ } /*else if (ms == 1) std::cout << "ms cannot decrease further\n";*/ break;
case SDLK_e: if (ms < 10) { ms += 1; /*std::cout << "ms increased to " << ms << "\n";*/ } /*else if (ms == 10) ms = 10; std::cout << "ms cannot increase further\n";*/ break;
case SDLKES: q = true; break;
}
}
}
}
// after main loop - usually nothing
// exit code
SDL_DestroyTexture(tb1); SDL_DestroyTexture(tb2); SDL_DestroyTexture(tb3); SDL_DestroyTexture(tb4); SDL_DestroyTexture(tb5); SDL_DestroyTexture(tb6); SDL_DestroyTexture(tb7); SDL_DestroyTexture(tex);
SDL_FreeSurface(ts1); SDL_FreeSurface(ts2); SDL_FreeSurface(ts3); SDL_FreeSurface(ts4); SDL_FreeSurface(ts5); SDL_FreeSurface(ts6); SDL_FreeSurface(ts7); SDL_FreeSurface(img);
TTF_CloseFont(font);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
TTF_Quit();
IMG_Quit();
SDL_Quit();
return 0;
}
Its because you keep loading more textures and surfaces without freeing them. You only need to load them once. Put your buffer setup before the loop, also check the memory usage when its running im almost certain its gonna be rising alot

General SDL2 problems. MacOS, Xode

What I'm trying to do is make an SDL program that can write special symbols to a window I created. The keyboard can change between 4 layers each registering 32 keys. I just can't figure out how to actually make something appear on the screen and have no idea what I'm doing wrong.
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, const char * argv[])
{
//Setup
bool quit = false;
SDL_Event event;
int z = 0; //Layer
int x = 0;
int y = 0;
//Init
SDL_Init(SDL_INIT_VIDEO);
SDL_Window * window = SDL_CreateWindow("TPKB", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 960, 640, SDL_WINDOW_SHOWN);
SDL_Rect rect = {x, y, 32, 32};
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface * A_srf = SDL_LoadBMP("A.bmp"); SDL_Texture * A = SDL_CreateTextureFromSurface(renderer, A_srf); SDL_FreeSurface(A_srf);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
//Events
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_KEYUP:
{
if(z == 0)
{
switch(event.key.keysym.sym)
{
//case SDLK_1: SDL_BlitSurface(A_srf, NULL, surface, srfrect); break;
case SDLK_2: SDL_RenderCopy(renderer, A, NULL, &rect); if(x == 928){x = 0; y += 32;} else{x += 32;} rect.x = x; rect.y = y; SDL_RenderClear(renderer); SDL_RenderPresent(renderer); break;
case SDLK_LEFT: z = 1; std::cout << "1"; break;
}
}
if(z == 1)
{
switch(event.key.keysym.sym)
{
case SDLK_UP: z = 0; std::cout << "0"; break;
}
}
}
}
}
//Cleanup
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(A);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I've tried moving the render clear render present stuff around but it hasn't seemed to be activating at all.

Ncurses issue around functions which print to a window

I'm currently making a simple ncurses menu for executing terminal commands more quickly while working on projects. I'm new to the library and keep having the same issue. I've read the documentation for the relevant functions but it seems to reveal nothing. I was hoping someone more experienced would know why there's an issue.
The function which performs "make main" has been the main one causing the problems
void makeMain( WINDOW *window ) {
wrefresh(window);
mvwprintw( window, 1, 1, "test" );
wrefresh(window);
std::system("make main");
wrefresh(window);
refresh();
wmove( window, 1, 1 );
refresh();
wrefresh( window );
}
the function is run from another function
void coordinateUserInput( int max, std::vector<command> commandslist, WINDOW *output ) {
cbreak();
noecho();
wrefresh(stdscr);
//prep work
char key_pressed;
bool can_go_up;
bool can_go_down;
int y;
int x;
const int &start_line = 3;
const int &finish_line = commandslist.size() + 2;
bool keep_going{1};
//
wmove( stdscr, 3, 2 );
refresh();
while(keep_going) {
getyx( stdscr, y, x );
can_go_up = (!(y == start_line));
can_go_down = (!(y == finish_line));
key_pressed = getch();
switch(key_pressed) {
case 'j':
if(can_go_down) {
wmove( stdscr, y + 1, 2 );
refresh();
}
break;
case 'k':
if(can_go_up) {
wmove( stdscr, y - 1, 2 );
refresh();
}
break;
case '\n':
((commandslist[y - 3]).fcnpointer)(output);
wmove( stdscr, 3, 2 );
refresh();
break;
case 'q':
keep_going = 0;
break;
default:
break;
}
}
return;
}
I'm fairly certain that these would be the problem functions. Running with -Wall and -Wextra doesn't yield errors.
int main() {
bool *is_set_up = new bool;
initscr();
refresh();
//set up command ouput window pointer
int maxx;
int *maxy = new int;
getmaxyx(stdscr, *maxy, maxx);
delete maxy;
static WINDOW *command_output = newwin( 3, maxx, 0, 0 );
box(command_output, 0, 0);
wrefresh(command_output);
//
std::vector<command> commands = {defineCommands()};
writeMenu(commands, commands.size(), is_set_up);
refresh();
coordinateUserInput(commands.size()-1, commands, command_output);
std::cout << "worked" << std::endl;
endwin();
delete is_set_up;
return 0;
}

SDL_Rect is larger than specified

I've gotten a weird error with SDL_Rect. Whenever I call SDL_RenderFillRect, with this rectangle:
SDL_Rect rect = {100, 100, 100, 100};
I always end up with the rectangle covering the entire 800 x 600 window.
Here's my program:
#include "SDL.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <thread>
#include <string>
class Window
{
public:
Window( std::string title, int width, int height );
void runEvents( SDL_Event event );
void drawRect( int x, int y, int h, int w );
bool closed = false;
SDL_Renderer *_renderer;
SDL_Window *_window;
private:
int colour = 0;
};
Window::Window( std::string title, int width, int height )
{
_window = SDL_CreateWindow( title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, 0 );
_renderer = SDL_CreateRenderer( _window, -1, 0 );
}
// this function deals with rects (it's supposed to draw a rect when the mouse button is down):
void Window::runEvents( SDL_Event event )
{
SDL_Rect rect = { 100, 100, 100, 100 };
switch( event.type )
{
case SDL_QUIT:
closed = true;
break;
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_ESCAPE:
closed = true;
break;
case SDLK_KP_1:
// set colour to blue
SDL_SetRenderDrawColor( _renderer, 0, 51, 204, 255 );
std::cout << "Colour set to blue!" << std::endl;
colour = 1;
break;
case SDLK_KP_2:
// set colour to red
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
std::cout << "Colour set to red!" << std::endl;
colour = 2;
break;
case SDLK_KP_3:
// set colour to green
SDL_SetRenderDrawColor( _renderer, 51, 204, 51, 255 );
std::cout << "Colour set to green!" << std::endl;
colour = 3;
break;
case SDLK_KP_4:
SDL_SetRenderDrawColor( _renderer, 153, 0, 153, 255 );
std::cout << "Colour set to purple!" << std::endl;
colour = 4;
break;
case SDLK_KP_0:
SDL_SetRenderDrawColor( _renderer, 255, 255, 255, 255 );
std::cout << "Colour set to white!" << std::endl;
colour = 0;
break;
}
break;
//this draws the rectangle:
case SDL_MOUSEBUTTONDOWN:
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
drawRect( rect.h, rect.y, rect.h, rect.w );
break;
//this removes it
case SDL_MOUSEBUTTONUP:
switch( colour )
{
case 1:
SDL_SetRenderDrawColor( _renderer, 0, 51, 204, 255 );
break;
case 2:
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
break;
case 3:
SDL_SetRenderDrawColor( _renderer, 51, 204, 51, 255 );
break;
case 4:
SDL_SetRenderDrawColor( _renderer, 153, 0, 153, 255 );
break;
case 0:
SDL_SetRenderDrawColor( _renderer, 255, 255, 255, 255 );
break;
}
drawRect( rect.h, rect.y, rect.h, rect.w );
break;
}
}
//this function is supposed to draw the rect
void Window::drawRect( int x, int y, int h, int w )
{
SDL_Rect rect = { x, y, w, h };
SDL_RenderFillRect( _renderer, &rect );
}
bool running = true;
void mouseCheck()
{
while( running )
{
int x;
int y;
SDL_GetMouseState( &x, &y );
printf( "Mouse is at x: %i and y: %i\n", x, y );
SDL_Delay( 5000 );
}
}
int main( int argc, char *argv[] )
{
SDL_Init( SDL_INIT_EVERYTHING );
Window window( "Hello World", 800, 600 );
SDL_Event event;
SDL_SetRenderDrawColor( window._renderer, 255, 255, 255, 255 );
std::thread mousePosition( mouseCheck );
mousePosition.detach();
while( !window.closed )
{
SDL_RenderClear( window._renderer );
SDL_RenderPresent( window._renderer );
if( SDL_PollEvent( &event ) )
{
window.runEvents( event );
}
}
return 0;
}
Multiple issues:
Right now your main draw loop is:
1. Draw
2. Clear
3. Present
You want:
1. (re)Set clear color
2. Clear
3. Draw
4. Present
Not sure what you're trying for with that mouseCheck() thread but avoid calling GUI-related SDL functions from other threads. If you need that info in another thread use thread-safe atomics/queues/messages to communicate it.
You're calling drawRect() in two places with rect.h as the first parameter instead of rect.x.
I'd recommend against interleaving input processing and rendering like you're doing in runEvents(). Update the system state (shouldDrawRect & colour) while draining the event queue (while(SDL_PollEvent())), then draw a frame with the new state (Window::drawScene()).
You're only processing a single event per frame. Drain the event queue each frame via while(SDL_PollEvent()) so that events don't get backed up and dropped.
All together:
#include "SDL.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <thread>
#include <string>
class Window
{
public:
Window( std::string title, int width, int height );
void runEvents( SDL_Event event );
void drawScene();
void drawRect( int x, int y, int h, int w );
bool closed = false;
SDL_Renderer *_renderer;
SDL_Window *_window;
private:
bool shouldDrawRect;
int colour = 0;
};
Window::Window( std::string title, int width, int height )
{
_window = SDL_CreateWindow( title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, 0 );
_renderer = SDL_CreateRenderer( _window, -1, 0 );
shouldDrawRect = false;
}
void Window::drawScene()
{
if( shouldDrawRect )
{
switch( colour )
{
case 0:
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
break;
case 1:
SDL_SetRenderDrawColor( _renderer, 0, 51, 204, 255 );
break;
case 2:
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
break;
case 3:
SDL_SetRenderDrawColor( _renderer, 51, 204, 51, 255 );
break;
case 4:
SDL_SetRenderDrawColor( _renderer, 153, 0, 153, 255 );
break;
}
SDL_Rect rect = { 100, 100, 100, 100 };
drawRect( rect.x, rect.y, rect.h, rect.w );
}
}
//this function deals with rects (it's supposed to draw a rect when the mouse button is down):
void Window::runEvents( SDL_Event event )
{
switch( event.type )
{
case SDL_QUIT:
closed = true;
break;
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_ESCAPE:
closed = true;
break;
case SDLK_KP_1:
// set colour to blue
std::cout << "Colour set to blue!" << std::endl;
colour = 1;
break;
case SDLK_KP_2:
// set colour to red
std::cout << "Colour set to red!" << std::endl;
colour = 2;
break;
case SDLK_KP_3:
// set colour to green
std::cout << "Colour set to green!" << std::endl;
colour = 3;
break;
case SDLK_KP_4:
std::cout << "Colour set to purple!" << std::endl;
colour = 4;
break;
case SDLK_KP_0:
std::cout << "Colour set to white!" << std::endl;
colour = 0;
break;
}
break;
//this draws the rectangle:
case SDL_MOUSEBUTTONDOWN:
shouldDrawRect = true;
break;
//this removes it
case SDL_MOUSEBUTTONUP:
shouldDrawRect = false;
break;
}
}
//this function is supposed to draw the rect
void Window::drawRect( int x, int y, int h, int w )
{
SDL_Rect rect = { x, y, w, h };
SDL_RenderFillRect( _renderer, &rect );
}
bool running = true;
int main( int argc, char *argv[] )
{
SDL_Init( SDL_INIT_EVERYTHING );
Window window( "Hello World", 800, 600 );
SDL_Event event;
while( !window.closed )
{
SDL_SetRenderDrawColor( window._renderer, 255, 255, 255, 255 );
SDL_RenderClear( window._renderer );
while( SDL_PollEvent( &event ) )
{
window.runEvents( event );
}
window.drawScene();
SDL_RenderPresent( window._renderer );
}
return 0;
}

ping-pong sdl game racket moving

I've been trying to make a simple ping-pong game by sdl libraries and now I'm facing a problem!
The game has 2 players, one racket moves by up and down keys, and the other moves by s and w keys.
The problem is e.g. I use up key, both rackets move, and I don't know where the problem is.Considering this rule that both players must be able to move their rackets at the same time.
I compile this code on Ubuntu.
It will be perfect if anyone helps me!
Thanks in advance!
#include <iostream>
#include "SDL/SDL.h"
#include <SDL/SDL_gfxPrimitives.h>
using namespace std;
int main()
{
SDL_Surface* screen = SDL_SetVideoMode(1200, 800 ,32, 0);
int i=0,j = 0;
while(true)
{
boxRGBA(screen, 1000, 200+j, 1050, 350+j, 0, 0, 0, 255);
SDL_Event event;
if(!SDL_PollEvent(&event));
{
if(event.type == SDL_QUIT)
return 0;
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_UP)
j += -5;
if(event.key.keysym.sym == SDLK_DOWN)
j += 5;
}
}
boxRGBA(screen, 1000, 200+j, 1050, 350+j, 255, 50, 0, 255);
SDL_Flip(screen);
SDL_Delay(20);
boxRGBA(screen, 100, 200+j, 50, 350+j, 0, 0, 0, 255);
SDL_Event event2;
if(!SDL_PollEvent(&event2));
{
if(event2.type == SDL_QUIT)
return 0;
if(event2.type == SDL_KEYDOWN)
{
if(event2.key.keysym.sym == SDLK_w)
j += -5;
if(event2.key.keysym.sym == SDLK_s)
j += 5;
}
}
boxRGBA(screen, 100, 200+j, 50, 350+j, 0,0, 255, 255);
SDL_Flip(screen);
SDL_Delay(20);
}
//////////////////////////////////////////////////////////
SDL_Delay(2000);
return 0;
}
The bracket locations are a pet-peeve of mine. However, that may have been modified when porting to SO.
You initialized integers i and j, but only variable j has been used.