gpt4 book ai didi

C、SDL : error: cannot convert 'SDL_Surface*' to 'SDL_Surface* (*)[15]

转载 作者:行者123 更新时间:2023-11-30 20:20:33 25 4
gpt4 key购买 nike

所以我正在编写一个用表面表填充屏幕的代码;这是代码:

main.c

#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif

#include <SDL/SDL.h>
#include <SDL_image.h>
#include "maploader.h"

#define W 510
#define H 510
#define SIZE 34

void pause();

int main ( int argc, char** argv )
{
SDL_Surface *screen = NULL;
SDL_Surface *surfaces[15][15];



SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(W, H, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption("demon game", NULL);
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
mapload(screen, surfaces[15][15], NULL);

SDL_Flip(screen);
pause();
SDL_QUIT;
return EXIT_SUCCESS;
}


void pause()
{
int continuer = 1;
SDL_Event event;

while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
}
}
}

maploader.c

#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif

#include <SDL/SDL.h>
#include <SDL_image.h>
#define W 510
#define H 510
#define SIZE 34



SDL_Surface *surfaces[15][15];


void mapload(SDL_Surface *screen, SDL_Surface *surfaces[][15], int lvl)
{
FILE *level = NULL;
char elements[125];
int i, j, k = 0;
SDL_Rect elementposition = {0,0};


level = fopen("level.txt", "r");
if (level == NULL)
{
exit(0);
}
fgets(elements, 125, level);
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
for (i=0; i<15; i++)
{
for (j=0; j<15; j++)
{
if (elements[k] == "0")
{
surfaces[i][j] = IMG_Load("mur.jpg");
}
else if (elements[k] == "1")
{
surfaces[i][j] = IMG_Load("caisse.jpg");
}
else if (elements[k] == "2")
{
surfaces[i][j] = IMG_Load("objectif.png");
}
else
{
surfaces[i][j] = NULL;
}
k++;

}

}

for (i=0; i<15; i++)
{
for (j=0; j<15; j++)
{
SDL_BlitSurface(surfaces[i][j], NULL, screen, &elementposition);
elementposition.x += SIZE;
}
elementposition.y += SIZE;
}




}

我从编译中得到的唯一错误如下:“无法将参数 '2' 的 'SDL_Surface*' 转换为 'SDL_Surface*' 到 'SDL_Surface* ()[15]' 到 'void mapload(SDL_Surface , SDL_Surface* (*)[15], int)'|"

显然,错误是从 mapload 函数的第二个参数初始化的,但我不清楚到底出了什么问题。有什么想法吗?

最佳答案

这个,

mapload(screen, surfaces[15][15], NULL);

应该是

mapload(screen, surfaces, NULL);

但现在你应该问自己,如果你不知道,那么可能,

  • void mapload(SDL_Surface *screen, SDL_Surface *surfaces[][15], int lvl) 的签名完全错误。
  • 你需要研究一下 中什么是数组以及它们与指针的关系。

请注意,surfaces[15][15] 表示第 16 指针数组中的第 16 元素,其中任何一个都不存在,因为您每个只分配了15个。所以你需要了解 中的数组,它们是如何分配的以及如何拥有动态数组。

此外,您告诉 的事实编译器认为函数需要数组与该函数内部不太相关,因此语法 SDL_Surface *surfaces[][15] 对于 C 程序员来说似乎很奇怪。

最后,由于surfaces是一个全局变量,您不需要将其作为参数传递,但是您应该问自己,它应该是一个全局变量?

关于C、SDL : error: cannot convert 'SDL_Surface*' to 'SDL_Surface* (*)[15],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45923744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com