我试图在这个突破克隆中使桨成为实际图像而不是矩形。我还想将砖块改成图像。我只是不知道如何使用像rects这样的图像,并试图找出它。
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <ctime>
//For cap fps
#define fps 60
//The screen attributes
const int SCREEN_WIDTH = 1008;
const int SCREEN_HEIGHT = 720;
const int SCREEN_BPP = 32;
//coordinates of ball
const int BALL_X = 500;
const int BALL_Y = 600;
//Declaring for color
Uint32 white;
//The surfaces
SDL_Surface *mainScreen = NULL;
SDL_Surface *difficultyScreen = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *result = NULL;
SDL_Surface *result2 = NULL;
//The event structure
SDL_Event event;
//Rects
SDL_Rect Paddle;
SDL_Rect Ball;
SDL_Rect Block1;
SDL_Rect Block2;
SDL_Rect Block3;
SDL_Rect Block4;
SDL_Rect Block5;
SDL_Rect Block6;
SDL_Rect Block7;
SDL_Rect Block8;
SDL_Rect Block9;
SDL_Rect Block10;
SDL_Rect Block11;
SDL_Rect Block12;
SDL_Rect Block13;
SDL_Rect Block14;
SDL_Rect Block15;
SDL_Rect Block16;
SDL_Rect Block17;
SDL_Rect Block18;
SDL_Rect Block19;
SDL_Rect Block20;
SDL_Rect Block21;
SDL_Rect Block22;
SDL_Rect Block23;
SDL_Rect Block24;
SDL_Rect Block25;
SDL_Rect Block26;
int xVel, yVel;
bool PointInRect(int x, int y, SDL_Rect rec)
{
if (x > rec.x && y > rec.y && x < rec.x + rec.w && y < rec.y + rec.h)
{
return true;
}
return false;
}
bool CheckCollision(SDL_Rect r1, SDL_Rect r2)
{
if (PointInRect(r1.x, r1.y, r2) == true ||
PointInRect(r1.x + r1.w, r1.y, r2) == true ||
PointInRect(r1.x, r1.y + r1.h, r2) == true ||
PointInRect(r1.x + r1.w, r1.y + r1.h, r2) == true)
{
return true;
}
return false;
}
/*bool ChackCollision(SDL_Rect r3, SDL_Rect r4)
{
if (PointInRect(r3.x, r3.y, r4) == true ||
PointInRect(r3.x + r3.w, r3.y, r4) == true)
{
return true;
}
return false;
}*/
void ResetBall()
{
Ball.x = BALL_X;
Ball.y = BALL_Y;
xVel = 3;
yVel = 3;
}
void LoadGame()
{
Paddle.x = 425;
Paddle.y = 650;
Paddle.h = 20;
Paddle.w = 150;
Ball.x = BALL_X;
Ball.y = BALL_Y;
Ball.h = 15;
Ball.w = 15;
Block1.x = 58;
Block1.y = 28;
Block1.h = 15;
Block1.w = 100;
Block2.x = 216;
Block2.y = 28;
Block2.h = 15;
Block2.w = 100;
Block3.x = 374;
Block3.y = 28;
Block3.h = 15;
Block3.w = 100;
Block4.x = 533;
Block4.y = 28;
Block4.h = 15;
Block4.w = 100;
Block5.x = 691;
Block5.y = 28;
Block5.h = 15;
Block5.w = 100;
Block6.x = 849;
Block6.y = 28;
Block6.h = 15;
Block6.w = 100;
Block7.x = 84;
Block7.y = 71;
Block7.h = 15;
Block7.w = 100;
Block8.x = 269;
Block8.y = 71;
Block8.h = 15;
Block8.w = 100;
Block9.x = 453;
Block9.y = 71;
Block9.h = 15;
Block9.w = 100;
Block10.x = 638;
Block10.y = 71;
Block10.h = 15;
Block10.w = 100;
Block11.x = 822;
Block11.y = 71;
Block11.h = 15;
Block11.w = 100;
Block12.x = 23;
Block12.y = 114;
Block12.h = 15;
Block12.w = 100;
Block13.x = 146;
Block13.y = 114;
Block13.h = 15;
Block13.w = 100;
Block14.x = 269;
Block14.y = 114;
Block14.h = 15;
Block14.w = 100;
Block15.x = 392;
Block15.y = 114;
Block15.h = 15;
Block15.w = 100;
Block16.x = 516;
Block16.y = 114;
Block16.h = 15;
Block16.w = 100;
Block17.x = 639;
Block17.y = 114;
Block17.h = 15;
Block17.w = 100;
Block18.x = 762;
Block18.y = 114;
Block18.h = 15;
Block18.w = 100;
Block19.x = 885;
Block19.y = 114;
Block19.h = 15;
Block19.w = 100;
Block20.x = 88;
Block20.y = 157;
Block20.h = 15;
Block20.w = 100;
Block21.x = 820;
Block21.y = 157;
Block21.h = 15;
Block21.w = 100;
Block22.x = 138;
Block22.y = 200;
Block22.h = 15;
Block22.w = 100;
Block23.x = 296;
Block23.y = 200;
Block23.h = 15;
Block23.w = 100;
Block24.x = 454;
Block24.y = 200;
Block24.h = 15;
Block24.w = 100;
Block25.x = 612;
Block25.y = 200;
Block25.h = 15;
Block25.w = 100;
Block26.x = 770;
Block26.y = 200;
Block26.h = 15;
Block26.w = 100;
white = SDL_MapRGB(screen->format, 255, 255, 255);
srand(time(NULL));
ResetBall();
}
void Logic()
{
SDL_Event occur;
SDL_PollEvent(&occur);
Uint8 *keystates = SDL_GetKeyState(NULL);
if (keystates[SDLK_LEFT])
{
Paddle.x -= 8;
}
if (keystates[SDLK_RIGHT])
{
Paddle.x += 8;
}
if (Paddle.x < 1)
{
Paddle.x = 1;
}
if (Paddle.x + Paddle.w > 1007)
{
Paddle.x = 1007 - Paddle.w;
}
//For ball movement
Ball.x += xVel;
Ball.y += yVel;
//Change the direction of the ball if ball hits the walls
if (Ball.y < 1)
{
yVel = -yVel;
}
if (Ball.x < 1)
{
xVel = -xVel;
}
if (Ball.x + Ball.w > 1007)
{
xVel = -xVel;
}
//Change the direction of the ball if ball hits the paddle
if (CheckCollision(Ball, Paddle) == true)
{
yVel = -yVel;
}
//Change the direction of the ball if ball hits the blocks
if (CheckCollision(Ball, Block1) == true)
{
yVel = -yVel;
Block1.x = -200;
Block1.y = -200;
}
if (CheckCollision(Ball, Block2) == true)
{
yVel = -yVel;
Block2.x = -200;
Block2.y = -200;
}
if (CheckCollision(Ball, Block3) == true)
{
yVel = -yVel;
Block3.x = -200;
Block3.y = -200;
}
if (CheckCollision(Ball, Block4) == true)
{
yVel = -yVel;
Block4.x = -200;
Block4.y = -200;
}
if (CheckCollision(Ball, Block5) == true)
{
yVel = -yVel;
Block5.x = -200;
Block5.y = -200;
}
if (CheckCollision(Ball, Block6) == true)
{
yVel = -yVel;
Block6.x = -200;
Block6.y = -200;
}
if (CheckCollision(Ball, Block7) == true)
{
yVel = -yVel;
Block7.x = -200;
Block7.y = -200;
}
if (CheckCollision(Ball, Block8) == true)
{
yVel = -yVel;
Block8.x = -200;
Block8.y = -200;
}
if (CheckCollision(Ball, Block9) == true)
{
yVel = -yVel;
Block9.x = -200;
Block9.y = -200;
}
if (CheckCollision(Ball, Block10) == true)
{
yVel = -yVel;
Block10.x = -200;
Block10.y = -200;
}
if (CheckCollision(Ball, Block11) == true)
{
yVel = -yVel;
Block11.x = -200;
Block11.y = -200;
}
if (CheckCollision(Ball, Block12) == true)
{
yVel = -yVel;
Block12.x = -200;
Block12.y = -200;
}
if (CheckCollision(Ball, Block13) == true)
{
yVel = -yVel;
Block13.x = -200;
Block13.y = -200;
}
if (CheckCollision(Ball, Block14) == true)
{
yVel = -yVel;
Block14.x = -200;
Block14.y = -200;
}
if (CheckCollision(Ball, Block15) == true)
{
yVel = -yVel;
Block15.x = -200;
Block15.y = -200;
}
if (CheckCollision(Ball, Block16) == true)
{
yVel = -yVel;
Block16.x = -200;
Block16.y = -200;
}
if (CheckCollision(Ball, Block17) == true)
{
yVel = -yVel;
Block17.x = -200;
Block17.y = -200;
}
if (CheckCollision(Ball, Block18) == true)
{
yVel = -yVel;
Block18.x = -200;
Block18.y = -200;
}
if (CheckCollision(Ball, Block19) == true)
{
yVel = -yVel;
Block19.x = -200;
Block19.y = -200;
}
if (CheckCollision(Ball, Block20) == true)
{
yVel = -yVel;
Block20.x = -200;
Block20.y = -200;
}
if (CheckCollision(Ball, Block21) == true)
{
yVel = -yVel;
Block21.x = -200;
Block21.y = -200;
}
if (CheckCollision(Ball, Block22) == true)
{
yVel = -yVel;
Block22.x = -200;
Block22.y = -200;
}
if (CheckCollision(Ball, Block23) == true)
{
yVel = -yVel;
Block23.x = -200;
Block23.y = -200;
}
if (CheckCollision(Ball, Block24) == true)
{
yVel = -yVel;
Block24.x = -200;
Block24.y = -200;
}
if (CheckCollision(Ball, Block25) == true)
{
yVel = -yVel;
Block25.x = -200;
Block25.y = -200;
}
if (CheckCollision(Ball, Block26) == true)
{
yVel = -yVel;
Block26.x = -200;
Block26.y = -200;
}
//Function to load image
SDL_Surface *load_Image( std::string filename )
{
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
loadedImage = SDL_LoadBMP(filename.c_str());
//If nothing went wrong in loading the image
if(loadedImage != NULL)
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat(loadedImage);
//Free the old surface
SDL_FreeSurface(loadedImage);
}
//Return the optimized image
return optimizedImage;
}
//Function to apply picture to screen
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface(source, clip, destination, &offset);
}
//Function to start SDL
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Name the window
SDL_WM_SetCaption( "Block Breaker", NULL );
//If everything initialized fine
return true;
}
//Function to load files
bool load_files()
{
//Load the image
mainScreen = load_Image( "Menu.bmp" );
difficultyScreen = load_Image( "Speed Menu.bmp" );
result = load_Image( "Results.bmp" );
result2 = load_Image( "Results2.bmp" );
return true;
}
//function to stop SDL
void clean_up()
{
SDL_FreeSurface(mainScreen);
SDL_FreeSurface(difficultyScreen);
SDL_FreeSurface(screen);
SDL_FreeSurface(result);
SDL_FreeSurface(result2);
//Quit SDL
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Variables
bool running = true;
bool menu = true;
bool difficulty = false;
bool mediumGame = false;
bool grayGameScreen = false;
bool hardGame = false;
bool quit = false;
bool Result = false;
bool Result2 = false;
//Initialize
if ( init() == false )
{
return 1;
}
//Quit function
if (quit == true)
{
SDL_Quit();
}
//Load the files
if ( load_files() == false )
{
return 1;
}
//Load Game
LoadGame();
Uint32 starting_tick;
//while the user hasn't quit
while (quit == false)
{
starting_tick = SDL_GetTicks();
//Menu screen
if (menu==true)
{
//Apply screen
apply_surface(0,0,mainScreen,screen);
//Loop to handle input by user
while(SDL_PollEvent(&event))
{
if (event.type==SDL_MOUSEBUTTONUP)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
int x = event.button.x;
int y = event.button.y;
//Button for difficulty screen
if ((x>370)&&(x<660)&&(y>265)&&(y<370))
{
menu = false;
difficulty = true;
SDL_Flip(screen);
}
if ((x>374)&&(x<660)&&(y>390)&&(y<500))
{
menu = false;
quit = true;
SDL_Flip(screen);
}
}
SDL_Flip(screen);
}
else if (event.type==SDL_QUIT)
{
quit = true;
}
}
SDL_Flip(screen);
}
//Difficulty screen
else if (difficulty==true)
{
//Apply surface
apply_surface(0,0,difficultyScreen,screen);
SDL_Flip(screen);
while (SDL_PollEvent(&event))
{
if (event.type==SDL_MOUSEBUTTONUP)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
int x = event.button.x;
int y = event.button.y;
//Button for medium game screen
if ((x>360)&&(x<660)&&(y>370)&&(y<465))
{
difficulty = false;
mediumGame = true;
SDL_Flip(screen);
}
//Button for hard game screen
if ((x>420)&&(x<610)&&(y>490)&&(y<575))
{
difficulty = false;
hardGame = true;
SDL_Flip(screen);
}
}
SDL_Flip(screen);
}
else if (event.type==SDL_QUIT)
{
quit = true;
}
}
}
//Medium game screen
else if (mediumGame == true)
{
//Fill color
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x68, 0x68, 0x68 ));
//Logic for the game
Logic();
//Place Paddle
SDL_FillRect(screen, &Paddle, white);
//Place Ball
SDL_FillRect(screen, &Ball, white);
//Place Blocks
SDL_FillRect(screen, &Block1, white);
SDL_FillRect(screen, &Block2, white);
SDL_FillRect(screen, &Block3, white);
SDL_FillRect(screen, &Block4, white);
SDL_FillRect(screen, &Block5, white);
SDL_FillRect(screen, &Block6, white);
SDL_FillRect(screen, &Block7, white);
SDL_FillRect(screen, &Block8, white);
SDL_FillRect(screen, &Block9, white);
SDL_FillRect(screen, &Block10, white);
SDL_FillRect(screen, &Block11, white);
SDL_FillRect(screen, &Block12, white);
SDL_FillRect(screen, &Block13, white);
SDL_FillRect(screen, &Block14, white);
SDL_FillRect(screen, &Block15, white);
SDL_FillRect(screen, &Block16, white);
SDL_FillRect(screen, &Block17, white);
SDL_FillRect(screen, &Block18, white);
SDL_FillRect(screen, &Block19, white);
SDL_FillRect(screen, &Block20, white);
SDL_FillRect(screen, &Block21, white);
SDL_FillRect(screen, &Block22, white);
SDL_FillRect(screen, &Block23, white);
SDL_FillRect(screen, &Block24, white);
SDL_FillRect(screen, &Block25, white);
SDL_FillRect(screen, &Block26, white);
//Loser
if (Ball.y + Ball.h > 719)
{
mediumGame = false;
Result2 = true;
SDL_Flip(screen);
}
//Winner
if (Block1.x != 58 &&
Block2.x != 216 &&
Block3.x != 374 &&
Block4.x != 533 &&
Block5.x != 691 &&
Block6.x != 849 &&
Block7.x != 84 &&
Block8.x != 269 &&
Block9.x != 453 &&
Block10.x != 638 &&
Block11.x != 822 &&
Block12.x != 23 &&
Block13.x != 146 &&
Block14.x != 269 &&
Block15.x != 392 &&
Block16.x != 516 &&
Block17.x != 639 &&
Block18.x != 762 &&
Block19.x != 885 &&
Block20.x != 88 &&
Block21.x != 820 &&
Block22.x != 138 &&
Block23.x != 296 &&
Block24.x != 454 &&
Block25.x != 612 &&
Block26.x != 770)
{
hardGame = false;
Result = true;
SDL_Flip(screen);
}
//Flip screen
SDL_Flip(screen);
}
//Hard game screen
else if (hardGame == true)
{
//Fill color
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x68, 0x68, 0x68 ));
//Logic for the game
Logic();
//Place Paddle
SDL_FillRect(screen, &Paddle, white);
//Place Ball
SDL_FillRect(screen, &Ball, white);
//Place Blocks
SDL_FillRect(screen, &Block1, white);
SDL_FillRect(screen, &Block2, white);
SDL_FillRect(screen, &Block3, white);
SDL_FillRect(screen, &Block4, white);
SDL_FillRect(screen, &Block5, white);
SDL_FillRect(screen, &Block6, white);
SDL_FillRect(screen, &Block7, white);
SDL_FillRect(screen, &Block8, white);
SDL_FillRect(screen, &Block9, white);
SDL_FillRect(screen, &Block10, white);
SDL_FillRect(screen, &Block11, white);
SDL_FillRect(screen, &Block12, white);
SDL_FillRect(screen, &Block13, white);
SDL_FillRect(screen, &Block14, white);
SDL_FillRect(screen, &Block15, white);
SDL_FillRect(screen, &Block16, white);
SDL_FillRect(screen, &Block17, white);
SDL_FillRect(screen, &Block18, white);
SDL_FillRect(screen, &Block19, white);
SDL_FillRect(screen, &Block20, white);
SDL_FillRect(screen, &Block21, white);
SDL_FillRect(screen, &Block22, white);
SDL_FillRect(screen, &Block23, white);
SDL_FillRect(screen, &Block24, white);
SDL_FillRect(screen, &Block25, white);
SDL_FillRect(screen, &Block26, white);
//Loser
if (Ball.y + Ball.h > 720)
{
hardGame = false;
Result2 = true;
SDL_Flip(screen);
}
//Winner
if (Block1.x != 58 &&
Block2.x != 216 &&
Block3.x != 374 &&
Block4.x != 533 &&
Block5.x != 691 &&
Block6.x != 849 &&
Block7.x != 84 &&
Block8.x != 269 &&
Block9.x != 453 &&
Block10.x != 638 &&
Block11.x != 822 &&
Block12.x != 23 &&
Block13.x != 146 &&
Block14.x != 269 &&
Block15.x != 392 &&
Block16.x != 516 &&
Block17.x != 639 &&
Block18.x != 762 &&
Block19.x != 885 &&
Block20.x != 88 &&
Block21.x != 820 &&
Block22.x != 138 &&
Block23.x != 296 &&
Block24.x != 454 &&
Block25.x != 612 &&
Block26.x != 770)
{
hardGame = false;
Result = true;
SDL_Flip(screen);
}
//Flip screen
SDL_Flip(screen);
}
//Win menu
else if (Result == true)
{
apply_surface(0,0,result,screen);
LoadGame();
SDL_Flip(screen);
while (SDL_PollEvent(&event))
{
if (event.type==SDL_MOUSEBUTTONUP)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
int x = event.button.x;
int y = event.button.y;
//Button for medium game screen
if ((x>360)&&(x<660)&&(y>370)&&(y<465))
{
Result = false;
difficulty = true;
SDL_Flip(screen);
}
//Button for hard game screen
if ((x>420)&&(x<610)&&(y>490)&&(y<575))
{
Result = false;
quit = true;
SDL_Flip(screen);
}
}
SDL_Flip(screen);
}
else if (event.type==SDL_QUIT)
{
quit = true;
}
}
}
//Lose menu
else if (Result2 == true)
{
apply_surface(0,0,result2,screen);
LoadGame();
SDL_Flip(screen);
while (SDL_PollEvent(&event))
{
if (event.type==SDL_MOUSEBUTTONUP)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
int x = event.button.x;
int y = event.button.y;
//Button for medium game screen
if ((x>360)&&(x<660)&&(y>370)&&(y<465))
{
Result = false;
difficulty = true;
SDL_Flip(screen);
}
//Button for hard game screen
if ((x>420)&&(x<610)&&(y>490)&&(y<575))
{
Result = false;
quit = true;
SDL_Flip(screen);
}
}
SDL_Flip(screen);
}
else if (event.type==SDL_QUIT)
{
quit = true;
}
}
}
//Cap fps
if (( 1000/fps) > SDL_GetTicks() - starting_tick)
{
SDL_Delay( 1000/fps - ( SDL_GetTicks() - starting_tick ));
}
}
//Clean up
clean_up();
return 0;
}
答案 0 :(得分:0)
以下是一些显示图片的示例代码:
加载图片:
SDL_Rect image_hitbox;
SDL_Texture* image = IMG_LoadTexture(renderer, filelocation.c_str());
现在,如果您希望图像以特定尺寸显示,而不考虑图像本身的大小,只需将其设置为正常:
image_hitbox.x = locationX;
image_hitbox.y = locationY;
image_hitbox.w = width;
image_hitbox.h = height;
如果您希望宽度和高度取决于图片本身,您可以这样做:
SDL_QueryTexture(image, nullptr, nullptr, &image_hitbox.w, &image_hitbox.h);
将获得纹理宽度和高度。
然后,要简单地渲染图像,请执行以下操作:
SDL_RenderCopy(renderer, image, nullptr, &image_hitbox);
SDL_RenderPresent(renderer);
将在image_hitbox的坐标处渲染图像(并将其缩放到设置的宽度和高度)。
我并非100%确定这正是您所要求的,特别是因为您似乎是渲染表面而不是纹理,但我建议使用纹理,因为它们使用的是GPU而不是CPU。