简体   繁体   English

如何使用 SDL2 和 SDL_RenderFillRect 渲染矩形?

[英]How can I render a rectangle using SDL2 and SDL_RenderFillRect?

I have been trying to make a pong clone as a first c++ "big" project, and I am encountering several problems.我一直在尝试将 pong 克隆作为第一个 C++“大”项目,但遇到了几个问题。 First of all, this is my code so far:首先,这是我到目前为止的代码:

game.hpp:游戏.hpp:

#pragma once
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>

class Game
{
public:
    Game();
    ~Game();

    // GAME VARIABLES
    const int WIDTH=720, HEIGHT=720, FONT_SIZE=32;
    int running, lastTime;
    int frameCount, timerFPS, lastFrame, fps;

    // GAME TOOLS
    SDL_Window *window;
    SDL_Renderer *renderer;
    TTF_Font *font;
    SDL_Color color;

    // GAME OBJECTS:
    SDL_Rect rPaddle, lPaddle, Ball, scoreBoard;

    // GAME FUNCTIONS:
    void initRects(); // AFTER YOU HAVE A WORKING VERSION, TRY TO MOVE TO CONSTRUCTOR
    void render();
    void run();
};

game.cpp:游戏.cpp:

#include "game.hpp"

Game::Game()
{
    running = 1;

    if(SDL_Init(SDL_INIT_EVERYTHING)<0)
        std::cerr << "SDL FAILED: SDL_INIT_EVERYTHING: " << SDL_GetError() << std::endl;
    if(SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer)<0)
        std::cerr << "SDL FAILED: SDL_CreateWindowAndRenderer: " << SDL_GetError() << std::endl;
    TTF_Init();
    font = TTF_OpenFont("PreschoolBits.ttf", FONT_SIZE);
    color.r=color.g=color.b=255;
}

Game::~Game()
{
    TTF_CloseFont(font);
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();
}   

void Game::initRects()
{
    lPaddle.x=32; lPaddle.h=HEIGHT/5;
    lPaddle.y=(HEIGHT/2)-(lPaddle.h/2);
    lPaddle.w=12;
    rPaddle=lPaddle;
    rPaddle.x=WIDTH-rPaddle.w-32;
    Ball.w=Ball.h=16;
}

void Game::render()
{
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);
    frameCount++;
    timerFPS=SDL_GetTicks()-lastFrame;
    if(timerFPS<(1000/60))
        SDL_Delay(timerFPS<((1000/60)-timerFPS));

    SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0);
    SDL_RenderFillRect(renderer, &lPaddle);
    SDL_RenderFillRect(renderer, &rPaddle);
    SDL_RenderFillRect(renderer, &Ball);

    SDL_RenderPresent(renderer);
}


void Game::run()
{
    initRects();
    while(running)
    {
        lastFrame=SDL_GetTicks();
        if(lastFrame>=(lastTime+1000))
        {
            lastTime=lastFrame;
            fps=frameCount;
            frameCount=0;
        }

        render();
        SDL_Delay(2000);
        running=0;
    }
}

pong.cpp:乒乓.cpp:

#include "game.hpp"

int main()
{
    Game game;

    game.run();
    return 0;
}

Now, when I try to render the render the rectangles, they don't show up.现在,当我尝试渲染矩形时,它们不显示。 So I tried to remove them and just render colors on screen.所以我试图删除它们并在屏幕上渲染颜色。

Both before and after the SDL_RenderClear I wrote:在 SDL_RenderClear 之前和之后我都写道:

SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);

But the window still showed up black, no matter what I tried.但无论我尝试什么,窗口仍然显示为黑色。

What could I be doing wrong?我可能做错了什么?

To make a filled Rect you must create a rect object: SDL_Rect MyRectName , Set its values to whatever you want: MyRectName.(w,h,x,y) = MyValue (These are defaulted to 0), and then to render, run SDL_RenderFillRect(MyRenderer, MyRectName);要制作一个填充的 Rect 你必须创建一个 rect 对象: SDL_Rect MyRectName ,将它的值设置为你想要的任何值: MyRectName.(w,h,x,y) = MyValue (这些默认为 0),然后渲染,运行SDL_RenderFillRect(MyRenderer, MyRectName); . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM