繁体   English   中英

为什么 SDL2 渲染器不向我的 window 绘制任何内容?

[英]Why doesn't the SDL2 Renderer draw anything to my window?

我目前正在 C 中开发 Chip-8 解释器。 对于渲染,我使用 SDL2 库。 问题是我无法在屏幕上绘制矩形。 SDL_RenderClear function 也不起作用。 也许是 Makefile 但我已经尝试将 linker 中的控制台标志更改为 windows。 谁能帮我?

我的代码:

main.c

#include "chip.h"

#define NO_ROM_INSERTED 1

const int PIXEL_SIZE = 20;
const int CHIP_WIDTH = 64;
const int CHIP_HEIGHT = 32;

const int SCREEN_WIDTH = CHIP_WIDTH * PIXEL_SIZE;
const int SCREEN_HEIGHT = CHIP_HEIGHT * PIXEL_SIZE;

int main(int argc, char **argv)
{
    SDL_Window *window = NULL;
    SDL_Renderer *renderer = NULL;
    SDL_Event event;
    int quit = 0;

    Chip *chip = (Chip*)malloc(sizeof(Chip));
    chipInitialize(chip);

    chip->gfx[23] = 0x01;

    /*Checks if a ROM file is inserted by an argument*/
    if(argc < 2)    
    {
        printf("Error, no Rom inserted");
        return NO_ROM_INSERTED;
    }
    else
    {
        printf("\n%s loaded into memory\n", argv[argc-1]);
    }

    loadProgramInMemory(chip, "./Roms/TETRIS");


    if(!init(window, renderer, SCREEN_WIDTH, SCREEN_HEIGHT))
    {
        printf( "Failed to initialize!\n" );
    }
    else
    {   
        while(!quit)
        {
            while(SDL_PollEvent(&event) != 0)
            {
                if(event.type == SDL_QUIT)
                {
                    quit = 1;
                }
            }

            // Clear screen
            SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0xFF, 0xFF);
            SDL_RenderClear(renderer);

            for(int i = 0; i < CHIP_HEIGHT; i++)
            {
                for(int j = 0; j < CHIP_WIDTH; j++)
                {
                    if(chip->gfx[i * CHIP_WIDTH + j] > 0x00) //Translates the one dimensional gfx to two dimensional screen
                    {
                        SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
                        SDL_Rect fillRect = {CHIP_WIDTH * i, CHIP_HEIGHT * j, PIXEL_SIZE, PIXEL_SIZE};
                        SDL_RenderFillRect(renderer, &fillRect);
                    }
                }
            }
            // Update screen
            SDL_RenderPresent(renderer);
        }
    }

    close(window, renderer);

    free(chip);

    return 0;
}

芯片.h

#include "window.h"

typedef struct chip{

unsigned char memory[4096];

unsigned short opcode;
unsigned short pc;
unsigned short stack[16];
unsigned short sp;

unsigned char gfx[64 * 32];

unsigned char V[16];
unsigned short I;

unsigned char delayTimer;
unsigned char soundTimer;

}Chip;

void chipInitialize(Chip *chip);
void loadFontInMemory(Chip *chip);
void loadProgramInMemory(Chip *chip, char *program);

芯片.c

#include "chip.h"

void chipInitialize(Chip *chip)
{
    chip->pc            = 0x200;            // Set program counter to 0x200
    chip->opcode        = 0x0000;           // Reset current opcode
    chip->I             = 0x0000;           // Reset index register
    chip->sp            = 0x0000;           // Reset stack pointer

    chip->delayTimer    = 0x00;             // Reset delay timer
    chip->soundTimer    = 0x00;             // Reset sound timer

    for(int i = 0; i < 64 * 32; i++)        // Reset the graphics
        chip->gfx[i] = 0x00;

    for(int i = 0; i < 16; i++)             // Reset the stack
        chip->stack[i] = 0x0000;

    for(int i = 0; i < 4069; i++)           // Reset memory
        chip->memory[i] = 0x00;

    for(int i = 0; i < 16; i++)             // Reset registers
        chip->V[i] = 0x00;

    loadFontInMemory(chip);                 // Loads the font-set into memory
}

void loadProgramInMemory(Chip *chip, char *program)
{
    int bufferSize = 0xFFF - 0x200;         // Size of the memory which is reserved for the program
    unsigned char buffer[bufferSize];
    FILE *ptr;

    ptr = fopen(program, "rb");
    fread(buffer, bufferSize, 1, ptr);

    for(int i = 0; i < bufferSize; i++)
    chip->memory[i + 512] = buffer[i];  // Reads in the program and stores it in memory at location 0x200 or 512
}

void loadFontInMemory(Chip *chip)
{
    // Zero                         // Six                          // C
    chip->memory[0x000] = 0xF0;     chip->memory[0x01E] = 0xF0;     chip->memory[0x03C] = 0xF0;
    chip->memory[0x001] = 0x90;     chip->memory[0x01F] = 0x80;     chip->memory[0x03D] = 0x80;
    chip->memory[0x002] = 0x90;     chip->memory[0x020] = 0xF0;     chip->memory[0x03E] = 0x80;
    chip->memory[0x003] = 0x90;     chip->memory[0x021] = 0x90;     chip->memory[0x03F] = 0x80;
    chip->memory[0x004] = 0xF0;     chip->memory[0x022] = 0xF0;     chip->memory[0x040] = 0xF0;

    // One                          // Seven                        // D
    chip->memory[0x005] = 0x20;     chip->memory[0x023] = 0xF0;     chip->memory[0x041] = 0xE0;
    chip->memory[0x006] = 0x60;     chip->memory[0x024] = 0x10;     chip->memory[0x042] = 0x90;
    chip->memory[0x007] = 0x20;     chip->memory[0x025] = 0x20;     chip->memory[0x043] = 0x90;
    chip->memory[0x008] = 0x20;     chip->memory[0x026] = 0x40;     chip->memory[0x044] = 0x90;
    chip->memory[0x009] = 0x70;     chip->memory[0x027] = 0x40;     chip->memory[0x045] = 0xE0;

    // Two                          // Eight                        // E
    chip->memory[0x00A] = 0xF0;     chip->memory[0x028] = 0xF0;     chip->memory[0x046] = 0xF0;
    chip->memory[0x00B] = 0x10;     chip->memory[0x029] = 0x90;     chip->memory[0x047] = 0x80;
    chip->memory[0x00C] = 0xF0;     chip->memory[0x02A] = 0xF0;     chip->memory[0x048] = 0xF0;
    chip->memory[0x00D] = 0x80;     chip->memory[0x02B] = 0x90;     chip->memory[0x049] = 0x80;
    chip->memory[0x00E] = 0xF0;     chip->memory[0x02C] = 0xF0;     chip->memory[0x04A] = 0xF0;

    // Three                        // Nine                         // F
    chip->memory[0x00F] = 0xF0;     chip->memory[0x02D] = 0xF0;     chip->memory[0x04B] = 0xF0;
    chip->memory[0x010] = 0x10;     chip->memory[0x02E] = 0x90;     chip->memory[0x04C] = 0x80;
    chip->memory[0x011] = 0xF0;     chip->memory[0x02F] = 0xF0;     chip->memory[0x04D] = 0xF0;
    chip->memory[0x012] = 0x10;     chip->memory[0x030] = 0x10;     chip->memory[0x04E] = 0x80;
    chip->memory[0x013] = 0xF0;     chip->memory[0x031] = 0xF0;     chip->memory[0x04F] = 0x80;

    // Four                         // A
    chip->memory[0x014] = 0x90;     chip->memory[0x032] = 0xF0;
    chip->memory[0x015] = 0x90;     chip->memory[0x033] = 0x90;
    chip->memory[0x016] = 0xF0;     chip->memory[0x034] = 0xF0;
    chip->memory[0x017] = 0x10;     chip->memory[0x035] = 0x90;
    chip->memory[0x018] = 0x10;     chip->memory[0x036] = 0x90;

    // Five                         // B
    chip->memory[0x019] = 0xF0;     chip->memory[0x037] = 0xE0;
    chip->memory[0x01A] = 0x80;     chip->memory[0x038] = 0x90;
    chip->memory[0x01B] = 0xF0;     chip->memory[0x039] = 0xE0;
    chip->memory[0x01C] = 0x10;     chip->memory[0x03A] = 0x90;
    chip->memory[0x01D] = 0xF0;     chip->memory[0x03B] = 0xE0;
}

window.h

#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>

int init(SDL_Window *window, SDL_Renderer* renderer, const 
int width, const int height);
void close(SDL_Window *window, SDL_Renderer* renderer);

window.c

#include "window.h"

int init(SDL_Window *window, SDL_Renderer *renderer, const 
int width, const int height)
{
//Initialization flag
int success = 1;

//Initialize SDL
if( SDL_Init(SDL_INIT_VIDEO) < 0)
{
    printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
    success = 0;
}
else
{
    //Create window
    window = SDL_CreateWindow( "Chip-8 Emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN );
    if( window == NULL )
    {
        printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError());
        success = 0;
    }
    else
    {
        renderer = SDL_CreateRenderer(window, -1, 0);
        if(renderer == NULL)
        {
            printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
            success = 0;
        }
        else
        {
            SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0xFF, 0xFF);
        }
    }
}

return success;
}

 void close(SDL_Window *window, SDL_Renderer* renderer)
{
//Destroy window
SDL_DestroyWindow( window );
window = NULL;

SDL_DestroyRenderer(renderer);
renderer = NULL;
//Quit SDL subsystems
SDL_Quit();
}

Makefile

OBJS = main.c chip.c window.c

OBJ_NAME = Emulator

all : $(OBJS)
gcc $(OBJS) -IC:\code\SDL2\i686-w64-mingw32\include -LC:\code\SDL2\i686-w64-mingw32\lib -w -Wl,-subsystem,console -lmingw32 -lSDL2main -lSDL2 -o $(OBJ_NAME)

让我从建议开始 - 如果您对答案感兴趣,请不要让回答问题变得比需要的更难。 想想它是如何寻找其他人的——一些大量的代码,似乎没有绘制任何东西; 为什么这个例子必须有 6 个文件? 为什么它依赖于我们没有的外部文件,因此实际上无法验证? 当然,只需初始化和渲染就可以将其缩小到 30 行的单个文件 - 实际上任何 SDL2 的“hello world”都可以。 当它起作用而你的代码不起作用时——你开始检查有什么不同。

现在回答你的问题。 您无法从主循环中绘制任何内容,因为您的window和此处的renderer是 NULL - 它已初始化为 NULL 并且从未设置为其他任何内容。 It appears you have misconception of how function arguments works - in C, funciton arguments are passed by value, and any modification of that value is made to local funciton copy. 当您传递一个 int 时,function 会获取该 int 的副本,并且不要将任何更改推回。 如果传递SDL_Window* ,则传递该指针的(在您的情况下为NULL ); 当您执行SDL_CreateWindow时,您将其分配给局部变量,并且一旦再次无法将其返回给调用者,它就会停留在 NULL 中。 因此,您所有的渲染调用都将 NULL 作为渲染器传递,这自然不是有效的渲染器。

同样的事情出现在您的close中-您正在尝试将传递的副本重置为 NULL,但这没有任何意义,因为您无法修改外部值,只能修改函数本地副本。

编程课程经常教导有“按值传递”和“按引用传递”,我认为这是造成这种混乱的根源。 我宁愿说 C 中没有传递引用,你总是传递值,但指针也是一个值。 为了能够修改数据,我们将指针传递给该数据; 要改变 int 你可以传递int* ,但是要改变SDL_Window*你需要获取指向指针的指针 - SDL_Window** 看看 SDL 本身是如何做到的: https://wiki.libsdl.org/SDL_CreateWindowAndRenderer

所以,简而言之 - 让你的init成为int init(SDL_Window **window, SDL_Renderer** renderer, int width, int height); (这里也不需要const int ,因为它们是宽度和高度的副本),并相应地修改其代码。 并在调用之后检查windowrenderer的值(调试器,调试 printf, if(window==NULL)一切正常)。 init可能类似于

int init(SDL_Window **owindow, SDL_Renderer **orenderer,
        int width, int height)
{
    SDL_Window *window = NULL;
    SDL_Renderer *renderer = NULL;

    //Initialization flag
    int success = 1;

    //Initialize SDL
    if( SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
        success = 0;
    }
    else
    {
        //Create window
        window = SDL_CreateWindow( "Chip-8 Emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN );
        if( window == NULL )
        {
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError());
            success = 0;
        }
        else
        {
            renderer = SDL_CreateRenderer(window, -1, 0);
            if(renderer == NULL)
            {
                printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
                success = 0;
            }
            else
            {
                SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0xFF, 0xFF);
            }
        }
    }

    *owindow = window;
    *orenderer = renderer;
    return success;
}

当然对init的调用必须修改为init(&window, &renderer, SCREEN_WIDTH, SCREEN_HEIGHT)

当我们这样做时,还有两件事:

  • Header 文件应该包含保护,否则如果您多次(直接或间接)包含它会导致问题。

  • 永远不要打电话给你的 function close 它会(没有任何关于它的可能)在 linux/UNIX 系统上崩溃。 想想另一个未被广泛使用的操作系统保留的名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM