• Create Account

# selve

Member Since 03 Nov 2012
Offline Last Active Mar 27 2013 08:32 PM

### [C++/SDL] Rotated image & collision detection

27 March 2013 - 10:26 AM

Hi guys,
I want to know how to rotate an image and make a collision with another object like this:

First collision it's just an easiest collision without rotating any image. When the ball collides with the square, the XVEL of the ball changes to
-XVEL.
Now the question for you: how to make a rotated image and collision with the ball that changes the xVelocity and yVelocity depending on the rotated image position.

### [C++] Delayed action of srand() - coordinates for object

21 November 2012 - 10:23 AM

Hi,
I've created the small program where the ball is moving all the time on the screen to random coordinates made like that:

[source lang="cpp"]srand(time(NULL)); do { ballX = randx; ballY = randy; }while(abs(oldx-ballX) > 300 && abs(oldy-ballY) > 300); oldx = ballX; oldy = ballY;[/source]
It's included in while(!gameover) function. That moves the ball:

[source lang="cpp"]void moveBall(){ if(ballposition.x > ballX) { ballposition.x -= ballspeed; } if(ballposition.x < ballX) { ballposition.x += ballspeed; } if(ballposition.y > ballY) { ballposition.y -= ballspeed; } if(ballposition.y < ballY) { ballposition.y += ballspeed; }}[/source]

The only problem is that the ball is moving to the right coordinates X and Y, then sometimes stays for a moment and then another move and so on. When I try to delete the "srand(time(NULL));" it causes the ball to move really fast in each direction but no lag. How can I fix this?

### [SDL/C++] Asking player for text

10 November 2012 - 06:26 PM

Hi guys.
I want to do in my SDL Game asking for text. For example when I'll come close to the doors, there will be a code and u will have to type it by a keyboard. How can I do it?

### [SDL/C++] Making menu/options in game - Problem

09 November 2012 - 03:41 PM

Hello,
I'm writing a RPG game, which will be actually the horror RPG. I've done the animation for player and all that based stuff, but now I wanted to do buttons on the left corner of the screen:

For example if I press by mouse "QUESTS", this appears on the center of the screen:

And then it's waiting for me to press for example ESCAPE to turn the picture off.
If I click options, I would like to have for example such buttons on screen like: CONTROLS, BACKGROUND MUSIC, FULLSCREEN or BACK:

And if I click CONTROLS, the last picture disappears and this one is on the screen:

Then it's waiting for me to press ESCAPE or another button on screen, for example BACK.
Here's the part of code:
[source lang="cpp"]if(event.type == SDL_MOUSEMOTION ) { mouseX = event.motion.x; mouseY = event.motion.y; } if(event.type == SDL_MOUSEBUTTONDOWN ) { //Buttons if(event.button.button == SDL_BUTTON_LEFT && ( mouseX >= buttonquestslocation.x && mouseX <= buttonquestslocation.x + buttonquestslocation.w ) && ( mouseY >= buttonquestslocation.y && mouseY <= buttonquestslocation.y + buttonquestslocation.h ) ) { Mix_PlayChannel(-1, paper, 0); SDL_BlitSurface(levelonemap, NULL, screen, &paperlocation); SDL_Flip(screen); //Here's the place where I want the screen to wait until the player will press the button. I didn't know how to do that so i put a Delay. SDL_Delay(3000); } if(event.button.button == SDL_BUTTON_LEFT && ( mouseX >= buttonoptionslocation.x && mouseX <= buttonoptionslocation.x + buttonoptionslocation.w ) && ( mouseY >= buttonoptionslocation.y && mouseY <= buttonoptionslocation.y + buttonoptionslocation.h ) ) { SDL_BlitSurface(options, NULL, screen, &optionslocation); SDL_BlitSurface(buttoncontrols, NULL, screen, &buttoncontrolslocation); SDL_Flip(screen); Mix_PlayChannel(-1, paper, 0); //Now player must choose between: CONTROLS, BACKGROUND MUSIC, FULLSCREEN or BACK } if(event.button.button == SDL_BUTTON_LEFT && ( mouseX >= buttonexitlocation.x && mouseX <= buttonexitlocation.x + buttonexitlocation.w ) && ( mouseY >= buttonexitlocation.y && mouseY <= buttonexitlocation.y + buttonexitlocation.h ) ) { SDL_Quit(); return 0; } }[/source]

### SDL - 2D Collision/Random Coordinates bug

03 November 2012 - 06:14 AM

Hi guys. Recently I started learning SDL. I wrote something like "Pickin' sticks". The only thing that's not working is I think the collision or maybe random coordinates of food. It is like this - I launch the game, pick some food (moving by arrows) and it's counting by int foodamount (you can see on the screen). Sometimes it works - I get +1 to foodamount and there's a single sound crunch. However sometimes when I try to pick it up, the sound of crunch is repeating and repeating, food appears in the same place on the floor, the foodamount counter rises from 5 to 300. I don't know how to fix this. I would be really grateful if you can help me. Screens are under the code. Here's the code (the code fuction didn't work for me on the forum):

#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_image.h"
#include <windows.h>
#include <stdbool.h>
#define randx rand()*100%1175
#define randy rand()*100%585
SDL_Surface * background;
SDL_Surface * player;
SDL_Surface * screen;
//ITEMS
SDL_Surface * hamburger;
SDL_Surface * banana;
SDL_Surface * fries;
SDL_Surface * donut;
SDL_Surface * cake;
SDL_Surface * item;
//ITEMS
SDL_Surface * koniec;
SDL_Event event;
SDL_Rect playerlocation;
//ITEMS LOCATIONS
SDL_Rect hamburgerlocation;
SDL_Rect bananalocation;
SDL_Rect frieslocation;
SDL_Rect donutlocation;
SDL_Rect cakelocation;
SDL_Rect itemlocation;
//ITEMS LOCATIONS
int fullscreen = 0;
int randomfood;
int foodamount;
void putfood()
{
srand( time( NULL ) );
int itemx,itemy;
itemx = randx;
itemy = randy;
randomfood = (rand()%(5+1)) + 1;
if(randomfood == 1)
{
hamburgerlocation.x = itemx;
hamburgerlocation.y = itemy;
hamburgerlocation.w = 40;
hamburgerlocation.h = 40;
item = hamburger;
itemlocation = hamburgerlocation;
SDL_BlitSurface(item, NULL, screen, &itemlocation);
//SDL_Flip(screen);
}
if(randomfood == 2)
{
bananalocation.x = itemx;
bananalocation.y = itemy;
bananalocation.w = 40;
bananalocation.h = 40;
item = banana;
itemlocation = bananalocation;
SDL_BlitSurface(item, NULL, screen, &itemlocation);
//SDL_Flip(screen);
}
if(randomfood == 3)
{
frieslocation.x = itemx;
frieslocation.y = itemy;
frieslocation.w = 40;
frieslocation.h = 40;
item = fries;
itemlocation = frieslocation;
SDL_BlitSurface(item, NULL, screen, &itemlocation);
//SDL_Flip(screen);
}
if(randomfood == 4)
{
donutlocation.x = itemx;
donutlocation.y = itemy;
donutlocation.w = 40;
donutlocation.h = 40;
item = donut;
itemlocation = donutlocation;
SDL_BlitSurface(item, NULL, screen, &itemlocation);
//SDL_Flip(screen);
}
if(randomfood == 5)
{
cakelocation.x = itemx;
cakelocation.y = itemy;
cakelocation.w = 40;
cakelocation.h = 40;
item = cake;
itemlocation = cakelocation;
SDL_BlitSurface(item, NULL, screen, &itemlocation);
//SDL_Flip(screen);
}
}
int collision(SDL_Rect* rect1,SDL_Rect* rect2)
{
if(rect1->y >= rect2->y + rect2->h)
return 0;
if(rect1->x >= rect2->x + rect2->w)
return 0;
if(rect1->y + rect1->h <= rect2->y)
return 0;
if(rect1->x + rect1->w <= rect2->x)
return 0;
return 1;
}
bool checkCollision(SDL_Rect *player, SDL_Rect *obj)
{
if(player->x < obj->x + obj->w && player->x + player->w > obj->x && player->y < obj->y + obj->h && player->y + player->h > obj->y)
{
return true;
}
else
{
return false;
}
}
int main(int argc, char * args[])
{
bool jump = false;
Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096);
Mix_VolumeChunk(intro, 64);
Mix_VolumeChunk(step, 10);
Mix_VolumeChunk(eat, 70);
Mix_PlayChannel(-1, intro, 0);
int x,y;
int exit = 0;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(1280,720,32,SDL_DOUBLEBUF|SDL_SWSURFACE);
//items
//items
SDL_WM_SetCaption("Program testowy SDL v2 (by Selve)", NULL);
Uint8 * keystate = SDL_GetKeyState( NULL );
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_Flip(screen);
x = 500;
y = 250;
playerlocation.w = 40;
playerlocation.h = 40;
playerlocation.x = x;
playerlocation.y = y;
TTF_Init();
TTF_Font * font = TTF_OpenFont("font/font.ttf", 45);
SDL_Surface * text;
SDL_Color text_color = {255, 255, 255};
char buf[50];
sprintf(buf, "Food amount: %d", foodamount);
SDL_Surface * textSurface = TTF_RenderText_Solid(font, buf, text_color);
SDL_BlitSurface(textSurface, NULL, screen, NULL);
SDL_Flip(screen);
putfood();
while (exit == 0)
{
char buf[50];
sprintf(buf, "Food amount: %d", foodamount);
SDL_Surface * textSurface = TTF_RenderText_Solid(font, buf, text_color);
SDL_BlitSurface(textSurface, NULL, screen, NULL);
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
SDL_Quit();
return 0;
}
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_q)
{
SDL_FreeSurface(background);
SDL_Flip( screen );
Mix_CloseAudio();
SDL_BlitSurface( koniec, NULL, screen, NULL );
SDL_Flip( screen );
SDL_Delay(2500);
SDL_Quit();
return 0;
}
if(event.key.keysym.sym == SDLK_RETURN && fullscreen == 0)
{
SDL_SetVideoMode( 1280, 720, 32, SDL_DOUBLEBUF|SDL_SWSURFACE|SDL_FULLSCREEN);
fullscreen = 1;
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(player, NULL, screen, &playerlocation);
SDL_BlitSurface(item, NULL, screen, &itemlocation);
SDL_Flip( screen );
break;
}
if(event.key.keysym.sym == SDLK_RETURN && fullscreen == 1)
{
SDL_SetVideoMode( 1280, 720, 32, SDL_DOUBLEBUF|SDL_SWSURFACE );
fullscreen = 0;
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(player, NULL, screen, &playerlocation);
SDL_BlitSurface(item, NULL, screen, &itemlocation);
SDL_Flip( screen );
break;
}
}
}
if(keystate[SDLK_RIGHT])
{
if(playerlocation.x < 1175)
{
playerlocation.x = playerlocation.x + 4;
Mix_PlayChannel(-1, step, 0);
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(item, NULL, screen, &itemlocation);
SDL_BlitSurface(textSurface, NULL, screen, NULL);
}
}
if(keystate[SDLK_LEFT])
{
if(playerlocation.x > 10)
{
Mix_PlayChannel(-1, step, 0);
playerlocation.x = playerlocation.x - 4;
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(item, NULL, screen, &itemlocation);
SDL_BlitSurface(textSurface, NULL, screen, NULL);
}
}
if(keystate[SDLK_UP])
{
if(playerlocation.y > 10)
{
Mix_PlayChannel(-1, step, 0);
playerlocation.y = playerlocation.y - 4;
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(item, NULL, screen, &itemlocation);
SDL_BlitSurface(textSurface, NULL, screen, NULL);
}
}
if(keystate[SDLK_DOWN])
{
if(playerlocation.y < 585)
{
Mix_PlayChannel(-1, step, 0);
playerlocation.y = playerlocation.y + 4;
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(item, NULL, screen, &itemlocation);
SDL_BlitSurface(textSurface, NULL, screen, NULL);
}
}
SDL_BlitSurface(player, NULL, screen, &playerlocation);
SDL_Flip(screen);
if(checkCollision(&playerlocation, &itemlocation))
{
putfood();
Mix_PlayChannel(1, eat, 0);
foodamount++;
}
}
}

Here's the photos:

And next:

PARTNERS