C++ Xcode与visualstudio-sdlc++;

C++ Xcode与visualstudio-sdlc++;,c++,xcode,visual-studio-2010,sdl,C++,Xcode,Visual Studio 2010,Sdl,我使用各种在线教程和几本书自学编程。目前是C++。在过去的几天里,我已经做了一点SDL和SDL 我有一个小程序,创建一堵墙来阻止一个小正方形通过它 这是我的密码: // // main.cpp // SDL_Template // // The headers #include <stdlib.h> #include <string> // SDL headers #include <S

我使用各种在线教程和几本书自学编程。目前是C++。在过去的几天里,我已经做了一点SDL和SDL

我有一个小程序,创建一堵墙来阻止一个小正方形通过它

这是我的密码:

    //
    //  main.cpp
    //  SDL_Template
    //

    // The headers
    #include <stdlib.h>
    #include <string>

    // SDL headers
    #include <SDL/SDL.h>
    #include "SDL_image/SDL_image.h"
    //#include "SDL/SDL_ttf.h"
    //#include "SDL/SDL_mixer.h"

    // Other headers
    #include <OpenGL/gl3.h>


    // Screen attributes
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;
    const int SCREEN_BPP = 32;

    // The frame rate
    const int FRAMES_PER_SECOND = 20;

    // The attributes of the square
    const int SQUARE_WIDTH = 20;
    const int SQUARE_HEIGHT = 20;

    // The surfaces
    SDL_Surface *square = NULL;
    SDL_Surface *screen = NULL;

    // The event structure
    SDL_Event event;

    // The wall
    SDL_Rect wall;

    // The square
    class Square
    {
        private:
            // The collision box of the square
            SDL_Rect box;

            // The velocity of the square
            int xVel, yVel;

        public:
            // Initializes the variables
            Square();

            // Takes key presses and adjusts the square's velocity
            void handle_input();

            // Moves the square
            void move();

            // Shows the square on the screen
            void show();
    };

    //The timer
    class Timer
    {
        private:
            // The clock time when the timer started
            int startTicks;

            // The ticks stored when the timer was paused
            int pausedTicks;

            // The timer status
            bool paused;
            bool started;

        public:
            // Initializes variables
            Timer();

            // The various clock actions
            void start();
            void stop();
            void pause();
            void unpause();

            // Gets the timer's time
            int get_ticks();

            // Checks the status of the timer
            bool is_started();
            bool is_paused();
    };

    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 surface 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 check_collision(SDL_Rect A, SDL_Rect B)
    {
        // The sides of the rectangles
        int leftA, leftB;
        int rightA, rightB;
        int topA, topB;
        int bottomA, bottomB;

        // Calculate the sides of rect A
        leftA = A.x;
        rightA = A.x + A.w;
        topA = A.y;
        bottomA = A.y + A.h;

        // Calculate the sides of rect B
        leftB = B.x;
        rightB = B.x + B.w;
        topB = B.y;
        bottomB = B.y + B.h;

        // If any of the sides from A are outside of B
        if( bottomA <= topB )
        {
            return false;
        }

        if( topA >= bottomB )
        {
            return false;
        }

        if( rightA <= leftB )
        {
            return false;
        }

        if( leftA >= rightB )
        {
            return false;
        }

        // If none of the sides from A are outside B
        return true;
    }

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

        // Set the window caption
        SDL_WM_SetCaption("Move the Square", NULL);

        // If everything initialized fine
        return true;
    }

    bool load_files()
    {
        // Load the square image
        square = load_image("square.bmp");

        // If there was a problem in loading the square
        if (square == NULL)
        {
            return false;
        }

        // If everything loaded fine
        return true;
    }

    void clean_up()
    {
        // Free the surface
        SDL_FreeSurface(square);

        // Quit SDL
        SDL_Quit();
    }

    Square::Square()
    {
        // Initialize the offsets
        box.x = 0;
        box.y = 0;

        // Set the square's dimentions
        box.w = SQUARE_WIDTH;
        box.h = SQUARE_HEIGHT;

        // Initialize the velocity
        xVel = 0;
        yVel = 0;
    }

    void Square::handle_input()
    {
        // If a key was pressed
        if (event.type == SDL_KEYDOWN)
        {
            //Adjust the velocity
            switch (event.key.keysym.sym)
            {
                case SDLK_UP: yVel -= SQUARE_HEIGHT / 2; break;
                case SDLK_DOWN: yVel += SQUARE_HEIGHT / 2; break;
                case SDLK_LEFT: xVel -= SQUARE_WIDTH / 2; break;
                case SDLK_RIGHT: xVel += SQUARE_WIDTH / 2; break;
            }
        }
        // If a key was released
        else if (event.type == SDL_KEYUP)
        {
            //Adjust the velocity
            switch (event.key.keysym.sym)
            {
                case SDLK_UP: yVel += SQUARE_HEIGHT / 2; break;
                case SDLK_DOWN: yVel -= SQUARE_HEIGHT / 2; break;
                case SDLK_LEFT: xVel += SQUARE_WIDTH / 2; break;
                case SDLK_RIGHT: xVel -= SQUARE_WIDTH / 2; break;
            }
        }
    }

    void Square::move()
    {
        // Move the square left or right
        box.x += xVel;

        // If the square went too far to the left or right or has collided with the wall
        if (( box.x < 0 ) || ( box.x + SQUARE_WIDTH > SCREEN_WIDTH ) || ( check_collision(box, wall)))
        {
            // Move back
            box.x -= xVel;
        }

        // Move the square up or down
        box.y += yVel;

        // If the square went too far up or down or has collided with the wall
        if (( box.y < 0 ) || ( box.y + SQUARE_HEIGHT > SCREEN_HEIGHT) || (check_collision(box, wall)))
        {
            // Move back
            box.y -= yVel;
        }
    }

    void Square::show()
    {
        // Show the square
        apply_surface(box.x, box.y, square, screen);
    }

    Timer::Timer()
    {
        // Initialize the variables
        startTicks = 0;
        pausedTicks = 0;
        paused = false;
        started = false;
    }

    void Timer::start()
    {
        // Start the timer
        started = true;

        // Unpause the timer
        paused = false;

        // Get the current clock time
        startTicks = SDL_GetTicks();
    }

    void Timer::stop()
    {
        // Stop the timer
        started = false;

        // Unpause the timer
        paused = false;
    }

    void Timer::pause()
    {
        // If the timer is running and isn't already paused
        if ((started == true) && (paused == false))
        {
            // Pause the timer
            paused = true;

            // Calculate the paused ticks
            pausedTicks = SDL_GetTicks() - startTicks;
        }
    }

    void Timer::unpause()
    {
        // If the timer is paused
        if (paused == true)
        {
            // Unpause the timer
            paused = false;

            // Reset the starting ticks
            startTicks = SDL_GetTicks() - pausedTicks;

            // Reset the paused ticks
            pausedTicks = 0;
        }
    }

    int Timer::get_ticks()
    {
        // If the timer is running
        if (started == true)
        {
            // If the timer is paused
            if (paused == true)
            {
                // Return the number of ticks when the timer was paused
                return pausedTicks;
            }
            else
            {
                // Return the current time minus the start time
                return SDL_GetTicks() - startTicks;
            }
        }

        // If the timer isn't running
        return 0;
    }

    bool Timer::is_started()
    {
        return started;
    }

    bool Timer::is_paused()
    {
        return paused;
    }

    int main(int argc, char* args[])
    {
        // Quit flag
        bool quit = false;

        // The square
        Square mySquare;

        // The frame rate regulator
        Timer fps;

        // Initialize
        if( init() == false )
        {
            return 1;
        }

        // Load the files
        if (load_files() == false)
        {
            return 1;
        }

        // Set the wall
        wall.x = 300;
        wall.y = 40;
        wall.w = 40;
        wall.h = 400;

        // While the user hasn't quit
        while (quit == false)
        {
            // Start the frame timer
            fps.start();

            // While there are events to handle
            while (SDL_PollEvent(&event))
            {
                // Handle events for the square
                mySquare.handle_input();

                // If the user has Xed out the window
                if (event.type == SDL_QUIT)
                {
                    // Quit the program
                    quit = true;
                }
            }

            // Move the square
            mySquare.move();

            // Fill the screen white
            SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));

            // Show the wall
            SDL_FillRect (screen, &wall, SDL_MapRGB(screen->format, 0x77, 0x77, 0x77));

            // Show the square on the screen
            mySquare.show();

            // 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;
    }
//
//main.cpp
//SDL_模板
//
//标题
#包括
#包括
//SDL头文件
#包括
#包括“SDL_图像/SDL_图像.h”
//#包括“SDL/SDL_ttf.h”
//#包括“SDL/SDL_混合器.h”
//其他标题
#包括
//屏幕属性
屏幕宽度=640;
屏幕上的常数=480;
屏幕常数(BPP=32);
//帧速率
const int FRAMES_PER_SECOND=20;
//正方形的属性
常数int平方_宽度=20;
常数平方英尺高度=20;
//表面
SDL_曲面*square=NULL;
SDL_表面*屏幕=空;
//事件结构
SDL_事件;
//墙
直墙;
//广场
阶级广场
{
私人:
//广场的碰撞盒
SDL矩形盒;
//正方形的速度
内特xVel,伊维尔;
公众:
//初始化变量
正方形();
//按下按键并调整正方形的速度
无效句柄_输入();
//移动正方形
无效移动();
//在屏幕上显示正方形
void show();
};
//计时器
班级计时器
{
私人:
//计时器启动时的时钟时间
int startTicks;
//计时器暂停时存储的滴答声
int暂停时间;
//计时器状态
布尔停顿了一下;
布尔开始;
公众:
//初始化变量
定时器();
//各种时钟动作
void start();
无效停止();
无效暂停();
无效取消暂停();
//获取计时器的时间
int get_ticks();
//检查计时器的状态
bool已启动();
布尔已暂停();
};
SDL_表面*加载_图像(标准::字符串文件名)
{
//加载的图像
SDL_表面*LoadeImage=NULL;
//将使用的优化曲面
SDL_曲面*optimizedImage=NULL;
//加载图像
LoadeImage=IMG_Load(filename.c_str());
//如果图像已加载
如果(LoadeImage!=NULL)
{
//创建优化的曲面
optimizedImage=SDL_显示格式(LoadeImage);
//释放旧表面
SDL_自由曲面(加载图像);
//如果曲面经过优化
如果(optimizedImage!=NULL)
{
//彩色键表面
SDL_SetColorKey(optimizedImage、SDL_SRCCOLORKEY、SDL_MapRGB(optimizedImage->format,0,0xFF,0xFF));
}
}
//返回优化的曲面
返回优化年龄;
}
void apply_surface(int x,int y,SDL_surface*源,SDL_surface*目标,SDL_Rect*剪辑=NULL)
{
//保留偏移量
垂直偏移量;
//得到补偿
偏移量x=x;
偏移量y=y;
//布利特
SDL_BlitSurface(源、剪辑、目标和偏移);
}
布尔检查碰撞(SDL矩形A、SDL矩形B)
{
//矩形的边
int leftA,leftB;
int rightA,rightB;
int topA,topB;
int bottomA,bottomB;
//计算矩形A的边
leftA=A.x;
右A=A.x+A.w;
topA=A.y;
底部A=A.y+A.h;
//计算矩形B的边
leftB=B.x;
右B=B.x+B.w;
topB=B.y;
底部B=B.y+B.h;
//如果A的任何一侧在B的外侧
if(bottomA=bottomB)
{
返回false;
}
if(rightA=rightB)
{
返回false;
}
//如果A的任何一侧都不在B之外
返回true;
}
boolinit()
{
//初始化所有SDL子系统
if(SDL_Init(SDL_Init_EVERYTHING)=-1)
{
返回false;
}
//设置屏幕
屏幕=SDL_设置视频模式(屏幕宽度、屏幕高度、屏幕BPP、SDL_表面);
//如果设置屏幕时出错
如果(屏幕==NULL)
{
返回false;
}
//设置窗口标题
SDL_WM_SetCaption(“移动正方形”,NULL);
//如果一切都好的话
返回true;
}
bool load_文件()
{
//加载方形图像
square=load_图像(“square.bmp”);
//如果装广场有问题
if(square==NULL)
{
返回false;
}
//如果一切顺利的话
返回true;
}
无效清除()
{
//释放表面
SDL_自由曲面(正方形);
//退出SDL
SDL_退出();
}
Square::Square()
{
//初始化偏移量
框x=0;
方框y=0;
//设置正方形的尺寸
box.w=方形宽度;
方框h=方形高度;
//初始化速度
xVel=0;
yVel=0;
}
void Square::handle_input()
{
//如果按了一个键
if(event.type==SDL\u KEYDOWN)
{
//调整速度
开关(event.key.keysym.sym)
{
案例SDLK向上:yVel-=方形高度/2;中断;
案例SDLK向下:yVel+=方形高度/2;中断;
案例SDLK_左:xVel-=方形宽度/2;中断;
案例SDLK_RIGHT:xVel+=SQUARE_WID
switch (event.key.keysym.sym)
{
     case SDLK_UP:    yVel -= SQUARE_HEIGHT / 2; break;
     case SDLK_DOWN:  yVel += SQUARE_HEIGHT / 2; break;
     case SDLK_LEFT:  xVel -= SQUARE_WIDTH  / 2; break;
     case SDLK_RIGHT: xVel += SQUARE_WIDTH  / 2; break;
     default: break;
}