尝试在SDL2.0中设置特定像素失败

时间:2017-03-13 02:08:15

标签: c rendering sdl-2

我正在尝试编写一个chip-8模拟器,并且已经将处理器全部包装好了。我正在尝试编写一个小程序来试验在SDL中渲染一个chip-8 ROM。然而,我遇到了障碍。在以下程序中:

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

const int SCREEN_HEIGHT = 320;
const int SCREEN_WIDTH = 640;

void put_pixel_xy(SDL_Renderer *renderer, int x, int y, unsigned char r, unsigned char g, unsigned char b) {
    SDL_SetRenderDrawColor(renderer, r, g, b, 255);
    SDL_RenderDrawPoint(renderer, x, y);
}

int main() {
    if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
        fprintf(stderr, "error: SDL could not initialize: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Window *window = SDL_CreateWindow("Chip-8 Emulator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

    if(window == NULL) {
        fprintf(stderr, "error: could not create SDL window: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    SDL_Delay(2000);

    for(int x = 0; x < SCREEN_WIDTH; ++x) {
        for(int y = 0; y < SCREEN_HEIGHT/2; ++y) {
            put_pixel_xy(renderer, x, y, 255, 255, 255);
        }
    }

    SDL_RenderPresent(renderer);

    SDL_Delay(5000);

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

我试图让屏幕的一半变白,半黑。但是,每当我运行此程序时,我都会得到以下结果:

SDL Result

任何帮助,为什么会发生这种情况将非常感激。谢谢!

0 个答案:

没有答案