我从SDL2开始,尝试理解SDL_Renderer是什么时遇到了一些麻烦。
它是什么?它有什么作用?SDL_Renderer,SDL_Window,SDL_Surface和SDL_Texture有什么区别以及它们之间的关系?
在尝试理解此入门代码时,我遇到了问题:
#include <iostream>
#include <SDL2/SDL.h>
int main()
{
/* Starting SDL */
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
/* Create a Window */
SDL_Window *window = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (window == nullptr) {
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
return 1;
}
/* Create a Render */
SDL_Renderer *render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (render == nullptr) {
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
return 1;
}
/* Load bitmap image */
SDL_Surface *bmp = SDL_LoadBMP("./Tutorial/res/Lesson1/hello.bmp");
if (bmp == nullptr) {
std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
return 1;
}
/* Upload surface to render, and then, free the surface */
SDL_Texture *texture = SDL_CreateTextureFromSurface(render, bmp);
SDL_FreeSurface(bmp);
if (texture == nullptr){
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
return 1;
}
/* Draw the render on window */
SDL_RenderClear(render); // Fill render with color
SDL_RenderCopy(render, texture, NULL, NULL); // Copy the texture into render
SDL_RenderPresent(render); // Show render on window
/* Wait 2 seconds */
SDL_Delay(5000);
/* Free all objects*/
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(render);
SDL_DestroyWindow(window);
/* Quit program */
SDL_Quit();
return 0;
}
我使用的是Twinklebear教程(在SDL Wiki上建议),并且在SDL Wiki文档和一些书上也有所查找。但是所有这些都假定我知道这些定义。