Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在SDL中用正方形绘制二维数组_C++_Sdl_Sdl 2 - Fatal编程技术网

C++ 在SDL中用正方形绘制二维数组

C++ 在SDL中用正方形绘制二维数组,c++,sdl,sdl-2,C++,Sdl,Sdl 2,我想用方块渲染一张桌子(比如桌面游戏,国际象棋)。 这是我的密码: #include <SDL.h> #include <stdio.h> SDL_Rect newSDL_Rect(int xs, int ys, int widths, int heights) { SDL_Rect rectangular; rectangular.x = xs; rectangular.y = ys; rectangular.w = widths;

我想用方块渲染一张桌子(比如桌面游戏,国际象棋)。 这是我的密码:

#include <SDL.h>
#include <stdio.h>
SDL_Rect newSDL_Rect(int xs, int ys, int widths, int heights)
{
    SDL_Rect rectangular;
    rectangular.x = xs;
    rectangular.y = ys;
    rectangular.w = widths;
    rectangular.h = heights;
    return rectangular;
}
int main(int argc, char* args[])
{
    SDL_Window* window = NULL;
    SDL_Surface* surface = NULL;
    SDL_Rect rects[15][13];
    if (SDL_Init(SDL_INIT_VIDEO) < 0) //Init the video driver
    {
        printf("SDL_Error: %s\n", SDL_GetError());
    }
    else
    {
        window = SDL_CreateWindow("SDL 2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN); //Creates the window
    if (window == NULL)
    {
        printf("SDL_Error: %s\n", SDL_GetError());
    }
    else
    {
        SDL_Renderer* renderer = NULL;
        renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED); //renderer used to color rects

        SDL_SetRenderDrawColor(renderer, 51, 102, 153, 255);
        SDL_RenderClear(renderer);

        for (int i = 0; i < 14; i++)
            for (int j = 0; j < 12; j++)
            {
                rects[i][j] = newSDL_Rect(20 + i*42, 20 + j*42, 40, 40);
                SDL_SetRenderDrawColor(renderer, 255, 102, 0, 255);
                SDL_RenderFillRect(renderer, &rects[i][j]);
            }

        SDL_UpdateWindowSurface(window);
        SDL_Delay(5000);
    }
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
#包括
#包括
SDL_Rect newSDL_Rect(整数x、整数y、整数宽度、整数高度)
{
SDL矩形;
矩形x=xs;
矩形y=ys;
矩形。w=宽度;
矩形。h=高度;
返回矩形;
}
int main(int argc,char*args[]
{
SDL_Window*Window=NULL;
SDL_Surface*Surface=NULL;
SDL_Rect rects[15][13];
if(SDL_Init(SDL_Init_VIDEO)<0)//初始化视频驱动程序
{
printf(“SDL_错误:%s\n”,SDL_GetError());
}
其他的
{
window=SDL_CreateWindow(“SDL 2”,SDL_WINDOWPOS_未定义,SDL_WINDOWPOS_未定义,640,480,显示SDL_window);//创建窗口
如果(窗口==NULL)
{
printf(“SDL_错误:%s\n”,SDL_GetError());
}
其他的
{
SDL_渲染器*渲染器=NULL;
renderer=SDL_CreateRenderer(窗口,0,SDL_renderer_ACCELERATED);//用于为矩形着色的渲染器
SDL_SetRenderDrawColor(渲染器,51、102、153、255);
SDL_渲染器(渲染器);
对于(int i=0;i<14;i++)
对于(int j=0;j<12;j++)
{
rects[i][j]=newSDL_Rect(20+i*42,20+j*42,40,40);
SDL_SetRenderDrawColor(渲染器,255、102、0、255);
SDL_RenderFillRect(renderer,&rects[i][j]);
}
SDL_更新内表面(窗口);
SDL_延迟(5000);
}
}
SDL_窗口(窗口);
SDL_退出();
返回0;
}
但是当我完全执行我的代码时,创建的窗口是空白的(全部为白色),持续5秒(因为SDL_延迟正在运行)。我不知道如何调试SDL,因为我是新手

我做错了什么?

您的阵列

SDL_Rect rects[14][12];
但您正在分别迭代15和13个元素。这比我们想象的要多。修正你的循环最大值


另一个错误是,您发布的代码在
newSDL\u Rect
中没有
return
(但有问题的代码有)。

您的
newSDL\u Rect
函数没有返回任何内容

SDL_Rect newSDL_Rect(int xs, int ys, int widths, int heights) {
SDL_Rect rectangular;
rectangular.x = xs;
rectangular.y = ys;
rectangular.w = widths;
rectangular.h = heights;
}
应该是:

SDL_Rect newSDL_Rect(int xs, int ys, int widths, int heights) {
SDL_Rect rectangular;
rectangular.x = xs;
rectangular.y = ys;
rectangular.w = widths;
rectangular.h = heights;
return rectangular;
}
以及:

(i=0;i<14;i++)的

对于(j=0;j<12;j++)

SDL\u更新内表面(窗口)

添加->
SDL_RenderPresent(渲染器)

SDL_延迟(5000)


newSDL\u Rect
中,您没有返回
rectangle
或者它只是一个打字错误?这是一个打字错误,很抱歉弄错了。我在没有它的情况下一直保留到15岁,13岁。那里不可能有错误。14是错误。只有14个元素,包括0,所以最后一个是13。啊,你说得对,对不起。但这并不能解决问题。它仍然只渲染一个白色窗口,但没有强制关闭(很明显,这是因为我试图在数组外部进行写操作)。它不是白色的,背景是蓝色的。这不存在问题,我只是用这个愚蠢的错误键入了一个旧版本,但是程序仍然停止了,没有呈现任何东西。你用什么IDE来运行代码?我想这可能就是问题所在。你犯了什么错误?我认为IDE不是问题所在。但我在windows上使用代码块v13.12和g++编译器。我一点也没有收到错误。当我运行它时,它只渲染一个白色窗口。我也在使用代码块,它是MinGW编译器。在你的原始版本中,只做上面的更改并重新编译你的代码。请更具体一些。
for ( i = 0; i < 14; i++)
for ( j = 0; j < 12; j++)