我有一个函数需要修改SDL_Rect类型的二维数组中的值。
因此,在原型中给出了参数:
SDL_Rect array[DIM1][DIM2];
void function(SDL_Rect *array[][DIM2]);
但是,当以这种方式调用该函数时:
function(array);
我收到不兼容的指针类型警告。
如果您能告诉我在哪里混淆,那就太好了。
下面的代码是一些实际的东西,用wallPosition数组和blitLevel函数。
Main.c:
#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include "movements.h"
#include "mapping.h"
#define WINDOW_HEIGHT 408
#define WINDOW_WIDTH 408
#define V_B 12 // V_B : VERTICAL_BLOCKS
#define H_B 12 // H_B : HORIZONTAL_BLOCKS
int main (int argc , char* argv[])
{
int noEvent = 1;
SDL_Surface *screen = NULL , *mario = NULL;
SDL_Rect marioPosition , wallPosition[H_B][V_B];
SDL_Event event;
marioPosition.x = 68;
marioPosition.y = 0;
if(SDL_Init(SDL_INIT_VIDEO) == -1)
{
fprintf(stderr , "SDL initialization error");
exit(EXIT_FAILURE);
}
SDL_WM_SetIcon(IMG_Load("images/mario_down.gif") , NULL);
screen = SDL_SetVideoMode(408 , 408 , 32 , SDL_HWSURFACE | SDL_DOUBLEBUF);
if(screen == NULL)
{
fprintf(stderr , "Unable to load video mode");
exit(EXIT_FAILURE);
}
SDL_FillRect(screen , NULL , SDL_MapRGB(screen->format , 0 , 0 , 0));
SDL_WM_SetCaption("Mario Sokoban (Yep)" , NULL);
mario = IMG_Load("images/mario_down.gif");
SDL_BlitSurface(mario , NULL , screen , &marioPosition);
blitLevel(screen , wallPosition);
SDL_Flip(screen);
SDL_EnableKeyRepeat(10, 5);
while(noEvent)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
noEvent = 0;
break;
case SDL_KEYDOWN:
moveMario(&mario , event , &noEvent , &marioPosition , wallPosition);
break;
}
SDL_FillRect(screen , NULL , SDL_MapRGB(screen->format , 0 , 0 , 0));
blitLevel(screen , wallPosition);
SDL_BlitSurface(mario , NULL , screen , &marioPosition);
SDL_Flip(screen);
}
SDL_Quit();
return EXIT_SUCCESS;
}
mapping.c:
#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include "movements.h"
#include "mapping.h"
#define WINDOW_HEIGHT 408
#define WINDOW_WIDTH 408
#define V_B 12 // V_B : VERTICAL_BLOCKS
#define H_B 12 // H_B : HORIZONTAL_BLOCKS
void blitLevel(SDL_Surface *screen , SDL_Rect *wallPosition[][V_B])
{
int i = 0 , j = 0 , v = 0 , w = 0;
FILE *mapFile = NULL;
SDL_Surface *wall = NULL;
wall = IMG_Load("images/wall.jpg");
mapFile = fopen("mapping.txt" , "r");
if(mapFile != NULL)
{
for(i = 0 ; i <= 408 ; i += 34)
{
for(j = 0 ; j <= 408 ; j += 34)
{
wallPosition[v][w]->x = i;
wallPosition[v][w]->y = j;
v ++;
w ++;
if(fgetc(mapFile) == '1')
SDL_BlitSurface(wall , NULL , screen , *wallPosition);
}
}
}
fclose(mapFile);
}
最佳答案
void function(SDL_Rect *array[][DIM2]);
此函数将指向
SDL_Rect
的2d指针数组作为参数。因此,它与您要传递的数组不同,在数组中,每个元素都是一个SDL_Rect
而不是SDL_Rect*
。正确的原型为
void function(SDL_Rect array[][DIM2]);
。此C FAQ可能对您有所帮助。