【发布时间】:2022-01-06 18:52:37
【问题描述】:
#include<iostream>
#include<string>
#include<vector>
#include<SDL2/SDL.h>
#include<SDL2/SDL_image.h>
const int WIDTH = 800;
const int HEIGHT = 640;
int main()
{
std::vector<SDL_Surface> *devImages = {};
SDL_Surface *windowSurface = NULL;
SDL_Event windowEvent;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow("Hello SDL World", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
windowSurface = SDL_GetWindowSurface(window);
if(window == NULL){
std::cout<<"Could not create window: "<<SDL_GetError()<< std::endl;
return 1;
}
if(!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)){
std::cout<<"Could not create window: "<<IMG_GetError()<< std::endl;
return 1;
}
SDL_Surface *img1 = IMG_Load("Test.png");
SDL_Surface *test = IMG_Load("Hi.png");
SDL_Surface *f = IMG_Load("F.png");
auto it = *devImages->insert(devImages->begin(), 3);
devImages->insert(it, 2);
// devImages->emplace_back(test);
// devImages->emplace_back(f);
while(true)
{
if(SDL_PollEvent(&windowEvent) && SDL_QUIT == windowEvent.type)
break;
//SDL_BlitSurface(imageSurface, NULL, windowSurface, NULL);
for(auto item: *devImages){
SDL_BlitSurface(&item, NULL, windowSurface, NULL);
}
SDL_UpdateWindowSurface(window);
}
//SDL_FreeSurface(imageSurface);
//dimageSurface = NULL;
windowSurface = NULL;
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
当我尝试遍历 Vector 时,我的代码给了我错误。 谁能告诉我如何遍历这个向量或者我做错了什么。 我试图在屏幕上渲染多个图像,我从 GitHub 上的 Sonar System 的 SDL 教程 4 开始。谢谢。
【问题讨论】:
-
声明
std::vector<SDL_Surface> *devImages = {};似乎很奇怪。你的意思是std::vector<SDL_Surface *> devImages = {};? -
现在可以了,谢谢。
-
很高兴听到。随意接受答案以结束问题。
标签: c++ xcode for-loop vector sdl