1

我试图只在屏幕上渲染一些像素,窗口出现但它只是全黑。(我的猜测是 i3-wm 对 X11/SDL 做了一些奇怪的事情?)。我也在使用 Ubuntu 16.10,SDL 2.2,用 gcc 编译,用 gbd 调试。

也许我只是从 SDL 文档中遗漏了一些东西。

代码:

#include "SDL.h"
#include <stdio.h>
#include <stdint.h>
#include <sys/mman.h> 


typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;

// Temp?
#define internal static
#define local_persist static
#define global_variable static
global_variable SDL_Texture *Texture;
global_variable void *BitmapMemory;  //Pixels
global_variable int BitmapWidth;     //Texture Width
global_variable int BitmapHeight;     //Texture Width
global_variable int BytesPerPixel = 4;

bool HandleEvent(SDL_Event*);
// Backbuffer Functions.
internal void SDLResizeTexture(SDL_Renderer*, int, int);
internal void SDLUpdateWindow(SDL_Window*, SDL_Renderer*);
internal void RenderGradient(int, int);

int main(int argc, char *argv[]){

    SDL_Init(SDL_INIT_VIDEO);
    // Create Window.
    SDL_Window *Window = SDL_CreateWindow("The Origin Of Evil",
                                          SDL_WINDOWPOS_UNDEFINED, 
                                          SDL_WINDOWPOS_UNDEFINED, 
                                          640, 480, 
                                          SDL_WINDOW_RESIZABLE);
    if(Window){
        // Create Renderer.
        SDL_Renderer *Renderer = SDL_CreateRenderer(Window, -1, 0);
        if(Renderer){
            bool Running = true;
            int Width, Height;
            SDL_GetWindowSize(Window, &Width, &Height);
            SDLResizeTexture(Renderer, Width, Height);
            int XOffset = 0;
            int YOffset = 0;
            while(Running){
                SDL_Event Event;
                while(SDL_PollEvent(&Event)){
                    if(HandleEvent(&Event)){
                        Running = false;
                    }
                }
                RenderGradient(XOffset, YOffset);
                SDLUpdateWindow(Window, Renderer);

                ++XOffset;
                YOffset += 2;
            }
        }else{
            //TODO: Logging
            printf("No Renderer.\n");
        }
    }else{
        //TODO: Logging
        printf("No Window.\n");
    }

    SDL_Quit(); 
    return 0;
}

// True if we want to quit the program.
bool HandleEvent(SDL_Event *Event){
    bool ShouldQuit = false;
    switch (Event->type) {
        case SDL_QUIT:
        {
            printf("SDL_Quit\n") ;
            ShouldQuit = true;
        }break;

        case SDL_WINDOWEVENT:
        {
            switch (Event->window.event) {
                case SDL_WINDOWEVENT_SIZE_CHANGED:
                {
                    printf("SDL_WINDOWEVENT_SIZE_CHANGED\n");
                    SDL_Window *Window = SDL_GetWindowFromID(Event->window.windowID);
                    SDL_Renderer * Renderer = SDL_GetRenderer(Window);
                    SDLResizeTexture(Renderer, Event->window.data1, Event->window.data2);
                }break;

                case SDL_WINDOWEVENT_FOCUS_GAINED:
                {
                    printf("SDL_WINDOWEVENT_FOCUS_GAINED\n");
                }break;

                case SDL_WINDOWEVENT_EXPOSED:
                {
                    SDL_Window *Window = SDL_GetWindowFromID(Event->window.windowID);
                    SDL_Renderer *Renderer = SDL_GetRenderer(Window);
                    SDLUpdateWindow(Window, Renderer);
                }break;
            }

        }break;
    }
    return(ShouldQuit);
}

internal void SDLResizeTexture(SDL_Renderer *Renderer, int Width, int Height){
    if(BitmapMemory){
        munmap(BitmapMemory, BitmapWidth * BitmapHeight * BytesPerPixel);
    }
    if(Texture){
        SDL_DestroyTexture(Texture);
    }
    Texture = SDL_CreateTexture(Renderer, SDL_PIXELFORMAT_ARGB8888,
                                 SDL_TEXTUREACCESS_STREAMING,
                                 Width,
                                 Height);
    BitmapWidth = Width;
    BitmapHeight = Height;
    BitmapMemory = mmap(0,
                   Width * Height * BytesPerPixel,
                   PROT_READ | PROT_WRITE,
                   MAP_ANONYMOUS | MAP_PRIVATE,
                   -1,
                   0);
}

internal void SDLUpdateWindow(SDL_Window *Window, SDL_Renderer *Renderer){
    SDL_UpdateTexture(Texture, 0, BitmapMemory, BitmapWidth * BytesPerPixel);
    SDL_RenderCopy(Renderer, Texture, 0, 0);
    SDL_RenderPresent(Renderer);
}

internal void RenderGradient(int BlueOffset, int GreenOffset){
    int Width = BitmapWidth;
    int Height = BitmapHeight;
    int Pitch = Width * BytesPerPixel;
    uint8 *Row = (uint8 *)BitmapMemory;
    for(int Y = 0; Y > BitmapHeight; ++Y){
        uint32 *Pixel = (uint32 *)Row;
        for (int X = 0; X < BitmapWidth; ++X) {
           uint8 Blue = (X + BlueOffset);
           uint8 Green = (Y + GreenOffset);
           *Pixel++ = ((Green << 8) | Blue);
        }
        Row += Pitch;
    }
}
4

1 回答 1

2

for(int Y = 0; Y > BitmapHeight; ++Y)循环甚至不会执行一次。我想这应该是<

于 2017-04-27T18:17:31.907 回答