SDL2 Renderer为什么不向我的窗口绘制任何东西?

问题描述 投票:-1回答:1

我目前正在使用C语言开发Chip-8解释器。对于渲染,我使用SDL2库。问题是我无法在屏幕上绘制矩形。 SDL_RenderClear函数也不起作用。也许是Makefile,但我已经尝试将链接器中的控制台标志更改为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;
}

chip.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);

chip.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)
c sdl-2
1个回答
0
投票

让我从一个建议开始-如果您对某个答案完全感兴趣,请不要轻易回答您的问题。考虑一下它如何寻找其他人-大量的代码,似乎在不绘制任何内容方面存在问题;为什么这个例子必须有6个文件?为什么它依赖于我们没有的外部文件,因此实际上没有办法进行验证?当然,仅通过初始化和渲染就可以将其缩小到30行的单个文件-实际上,SDL2的任何“ hello world”都可以做到。并且当它起作用并且您的代码无效时–您开始检查有什么不同。

现在您的问题。您无法从主循环中绘制任何内容,因为此处的windowrenderer为NULL-它已初始化为NULL,并且从未设置为其他任何内容。似乎您对函数参数的工作原理有误解-在C语言中,函子参数按值传递,并且对该值的任何修改都对本地函子副本进行。传递一个int时,函数将获取该int的副本,并且不要将任何更改推回。如果传递SDL_Window*,则传递该指针的value(在您的情况下为NULL);当您执行SDL_CreateWindow时,将其分配给局部变量,并且一旦再次获得就无法将其返回给调用方,该调用方将保持NULL。因此,您所有的渲染调用都将NULL作为渲染器传递,这自然不是有效的渲染器。

close中出现相同的内容-您试图将传递的副本重置为NULL,但这没有意义,因为您不能修改外部值,只能修改函数本地副本。

编程班经常教导说,存在“按价值传递”和“按引用传递”,我认为这是造成这种混淆的根源。我宁愿说C中没有传递引用,您总是传递值,但是指针也是一个值。为了能够修改数据,我们将指针传递给该数据。要更改int,您可以传递int*,但要更改SDL_Window*,您需要获取指向that指针-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)

虽然我们在做,还有两件事:

  • 头文件应具有include guards,否则如果您多次(直接或间接)将其包含,则会引起问题。

  • 永远不要调用函数close。在linux / UNIX系统上,它将崩溃(没有任何可能的可能性)。考虑一下流行的操作系统无法保留的另一个名称。

© www.soinside.com 2019 - 2024. All rights reserved.