【问题标题】:How do I move a rectangle across a screen with SDL2.0 in C++?如何在 C++ 中使用 SDL2.0 在屏幕上移动矩形?
【发布时间】:2015-11-14 07:45:23
【问题描述】:

我正在寻找使用 SDL 围绕另一个正方形移动一个正方形。我正在遍历一系列值并使用这些值作为我的一个矩形的位置进行渲染。我有另一个静止的矩形,我目前每次循环时都会重新渲染静止的矩形,但我想消除它,因为我知道它效率不高。

#include "render.h"
#include "SDL2/SDL.h"
#include <iostream>
#include <fstream>

using namespace std;

const string FILE_NAME = "Orbit.txt";

const int WINDOW_WIDTH = 1280;
const int WINDOW_HEIGHT = 800;

const int SUN_LENGTH = 40;

const int EARTH_LENGTH = 20;

int main() {

    //Initialize SDL
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *window;
    SDL_Renderer *renderer;

    window = SDL_CreateWindow(
                              "test",                   //title
                              SDL_WINDOWPOS_CENTERED,   //initial x position
                              SDL_WINDOWPOS_CENTERED,   //initial y position
                              WINDOW_WIDTH,             //width
                              WINDOW_HEIGHT,            //height
                              0                         //flags
    );

    if (window == NULL) {
        // In the case that the window could not be made...
        printf("Could not create window: %s\n", SDL_GetError());
        return 1;
    }

    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    ifstream file;

    file.open(FILE_NAME);

    //Prepare render loop
    string trash;
    string time;
    double xPos;
    double yPos;

    int shorterEdge;

    if (WINDOW_HEIGHT < WINDOW_WIDTH) {
        shorterEdge = WINDOW_HEIGHT;
    } else {
        shorterEdge = WINDOW_WIDTH;
    }

    int numPixelsAU = (shorterEdge/2) - (EARTH_LENGTH/2) - 5;

    SDL_Rect sun;

    sun.x = ((WINDOW_WIDTH/2) - (SUN_LENGTH/2));
    sun.y = ((WINDOW_HEIGHT/2) - (SUN_LENGTH/2));
    sun.w = SUN_LENGTH;
    sun.h = SUN_LENGTH;

    SDL_Rect earth;

    //Render loop
    while (file >> trash >> time >> trash >> xPos >> trash >> yPos) {
        //Clear previous render
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);
        //Render Sun
        SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
        SDL_RenderDrawRect(renderer, &sun);
        SDL_RenderFillRect(renderer, &sun);
        //Render Earth
        earth.x = ((sun.x + (SUN_LENGTH/2)) + (numPixelsAU*xPos) - (EARTH_LENGTH/2));
        earth.y = ((sun.y + (SUN_LENGTH/2)) - (numPixelsAU*yPos) - (EARTH_LENGTH/2));
        earth.w = EARTH_LENGTH;
        earth.h = EARTH_LENGTH;
        SDL_SetRenderDrawColor(renderer, 30, 144, 255, 255);
        SDL_RenderDrawRect(renderer, &earth);
        SDL_RenderFillRect(renderer, &earth);
        SDL_RenderPresent(renderer);
        SDL_Delay(50);
    }

    SDL_Delay(3000);

    // Close and destroy the window
    SDL_DestroyWindow(window);

    // Clean up
    SDL_Quit();
    return 0;
}

【问题讨论】:

    标签: c++ sdl sdl-2


    【解决方案1】:

    我想你的问题是你应该如何计算地球的位置才能绕太阳运行?

    您必须计算每一帧的 posX 和 posY。假设每次渲染需要 50 毫秒(基于您使用的 SDL_Delay),如果您想要一个完整的轨道需要 3 秒,您将不得不使地球每帧 pi/30 弧度。所以你的角度将从 0 开始,然后每一帧都添加 pi/30。

    posX = cos(角度) * 半径

    posY = sin(角度) * 半径

    【讨论】:

    • 对不起,应该更清楚。我不担心位置,那不是问题。该程序按原样运行良好,但现在我每次在 while 循环中循环时都会渲染太阳。有没有办法保持太阳渲染,但只在我循环时改变地球?
    • 现代图形硬件更喜欢绘制整个场景,而不是使用脏矩形之类的东西来保留屏幕的一部分。
    猜你喜欢
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多