// main.cpp - Plane Battle Alpha final project main entry
// Written by Zhen Sun
#include "SDL.h"
#include "SDL_image.h"
#include <string>
//#include "Display.h"
#include "PPlane.h"
#include "Timer.h"
#include "PBullet.h"
// The screen attributes
#define SCREEN_WIDTH 310
#define SCREEN_HEIGHT 640
#define SCREEN_BPP 32
#define CAPTION "Plane Battle Game"
// The frame rate
#define FRAMES_PER_SECOND 20
// The surfaces
SDL_Surface *pPlane = NULL;
SDL_Surface *pBullet = NULL;
SDL_Surface *screen = NULL;
// The event structure
SDL_Event event;
SDL_Surface *load_image(std::string filename)
{
// The image that's loaded
SDL_Surface* loadedImage = NULL;
// The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
// Load the image
loadedImage = IMG_Load(filename.c_str());
// If the image loaded
if (loadedImage != NULL)
{
// Create an optimized surface
optimizedImage = SDL_DisplayFormat(loadedImage);
// Free the old surface
SDL_FreeSurface(loadedImage);
// If the surfaced was optimized
if (optimizedImage != NULL)
{
// Color key surface
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF));
}
}
// Return the optimized surface
return optimizedImage;
}
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);
}
bool init()
{
// Initialize all SDL substems
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;
}
// Set the window caption
SDL_WM_SetCaption(CAPTION, NULL);
// If everything initialized fine
return true;
}
void load_files(SDL_Surface *surfaceToLoad, std::string fileName)
{
// Load the PPlane image
surfaceToLoad = load_image(fileName);
// If there was a problem in loading the PPlane
if (surfaceToLoad == NULL)
system("pause");
// If everything loaded fine
// Doing Nothing
}
void clean_up()
{
// Free the surface
SDL_FreeSurface(pPlane);
SDL_FreeSurface(pBullet);
// Quit SDL
SDL_Quit();
}
// Main function
int main(int argc, char* args[])
{
// Quit flag
bool quit = false;
// The PPlane the will be used
PPlane myPlane;
PBullet myBullet;
// The frame rate regulator
Timer fps;
// Initialize
if (init() == false)
{
return 1;
}
// Load the files
pPlane = load_image("Player's Plane.bmp");
pBullet = load_image("Player's Bullet.bmp");
//load_files(pPlane, "Player's Plane.bmp");
//load_files(pBullet, "Player's Bullet.bmp");
// While the user hasn't quit
while (quit == false)
{
// Start the frame timer
fps.start();
// While there's event to handls
while(SDL_PollEvent(&myPlane.event))
{
// Handle events for the PPlane
myPlane.handle_input();
// If the user has Xed out the window
if (myPlane.event.type == SDL_QUIT)
{
// Quit the program
quit = true;
}
}
// Move the PPlane
myPlane.move();
myBullet.move();
// Fill the screen white
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
// Show the PPlane on the screen
apply_surface(myPlane.x, myPlane.y, pPlane, screen);
apply_surface(myBullet.x, myBullet.y, pBullet, screen);
// Clean up the bullet if it goes out of the screen
if (myBullet.y <= 0)
{
SDL_FreeSurface(pBullet);
}
// Update the screen
if (SDL_Flip(screen) == -1)
{
return 1;
}
// Cap the frame rate
if (fps.get_ticks() < 1000 / FRAMES_PER_SECOND)
{
SDL_Delay((1000 / FRAMES_PER_SECOND) - fps.get_ticks());
}
}
// Clean up
clean_up();
return 0;
}
SDL飞机大战template
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
GrayZS
- 粉丝: 0
- 资源: 1
最新资源
- zaopingshujufenxi.zip
- 六级作文模板万能句型pdf.zip
- delphi 12 控件之DISQLite3 v5.48.3 for Delphi 11-12 Athens + CRACK.7
- Delphi 12 控件之DIXml v6.3.1 for Delphi 11-12 Athens + CRACK.7z
- Delphi 12 控件Mitov Software MegaLab 8.0 for D12 Athens Source Cod
- Delphi 12 控件之Delphi-WinTaskSched-master.zip
- C语言连接两个字符串pdf.zip
- Matlab的语言教程
- Sigrity-PowerSI提取S参数仿真操作指导
- 基于Boson的计算机网络实验:静态路由的配置
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)