<commit_before>#define SDL_MAIN_HANDLED
#define GLEW_STATIC

#include <SDL.h>
#include <gl/glew.h>
#include <assert.h>
#include <vector>
#include "nes.h"

class Application
{
public:
    Application();
    ~Application();
    void run();
    void terminate();

private:
    bool create();
    void destroy();
    void handleEvents();
    void update();
    void render();

    SDL_Window*             mWindow;
    SDL_Surface*            mScreen;
    SDL_Surface*            mSurface;
    bool                    mValid;
    NES::Rom*               mRom;
    NES::Context*           mContext;
};

Application::Application()
    : mWindow(nullptr)
    , mScreen(nullptr)
    , mSurface(nullptr)
    , mValid(false)
{
}

Application::~Application()
{
    destroy();
}

void Application::run()
{
    mValid = create();
    while (mValid)
    {
        handleEvents();
        update();
        render();
    }
    destroy();
}

void Application::terminate()
{
    mValid = false;
}

bool Application::create()
{
    mWindow = SDL_CreateWindow("ManyNES", 100,
        100, 512, 480, SDL_WINDOW_SHOWN);
    if (!mWindow)
        return false;

    mScreen = SDL_GetWindowSurface(mWindow);
    if (!mScreen)
        return false;

    mSurface = SDL_CreateRGBSurface(0, 256, 256, 8, 0, 0, 0, 0);
    if (!mSurface)
        return false;

    static const SDL_Color colors[64] =
    {
#include "Palette.h"
    };
    SDL_SetPaletteColors(mSurface->format->palette, colors, 0, 64);

    mRom = NES::Rom::load("mario.nes");
    if (!mRom)
        return false;

    mContext = NES::Context::create(*mRom);
    if (!mContext)
        return false;

    return true;
}

void Application::destroy()
{
    if (mContext)
    {
        mContext->dispose();
        mContext = nullptr;
    }

    if (mRom)
    {
        mRom->dispose();
        mRom = nullptr;
    }

    if (mWindow)
    {
        SDL_DestroyWindow(mWindow);
        mWindow = nullptr;
    }

    if (mSurface)
    {
        SDL_FreeSurface(mSurface);
        mSurface = nullptr;
    }
}

void Application::handleEvents()
{
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        if (event.type == SDL_QUIT)
            terminate();

        if (event.type == SDL_KEYDOWN)
        {
            switch (event.key.keysym.sym)
            {
            case SDLK_ESCAPE:
                terminate();
                break;
            }
        }
    }
}

void Application::update()
{
    if (SDL_LockSurface(mSurface) == 0)
    {
        uint8_t* buffer = static_cast<uint8_t*>(mSurface->pixels);
        for (uint32_t y = 0; y < 256; ++y)
        {
            for (uint32_t x = 0; x < 256; ++x)
            {
                *buffer++ = static_cast<uint8_t>(x * y) & 63;
            }
        }

        mContext->update(mSurface->pixels, mSurface->pitch);

        SDL_UnlockSurface(mSurface);
    }
}

void Application::render()
{
    SDL_Rect rect = { 0, 0, 256, 256 };
    SDL_BlitSurface(mSurface, nullptr, mScreen, &rect);

    SDL_UpdateWindowSurface(mWindow);
}

int main()
{
    Application application;
    application.run();
    return 0;
}<commit_msg>Wait VSync for blitting surface to window.<commit_after>#define SDL_MAIN_HANDLED
#define GLEW_STATIC

#include <SDL.h>
#include <gl/glew.h>
#include <assert.h>
#include <vector>
#include "nes.h"

class Application
{
public:
    Application();
    ~Application();
    void run();
    void terminate();

private:
    bool create();
    void destroy();
    void handleEvents();
    void update();
    void render();

    SDL_Window*             mWindow;
    SDL_Renderer*           mRenderer;
    SDL_Surface*            mScreen;
    SDL_Surface*            mSurface;
    bool                    mValid;
    NES::Rom*               mRom;
    NES::Context*           mContext;
};

Application::Application()
    : mWindow(nullptr)
    , mRenderer(nullptr)
    , mScreen(nullptr)
    , mSurface(nullptr)
    , mValid(false)
{
}

Application::~Application()
{
    destroy();
}

void Application::run()
{
    mValid = create();
    while (mValid)
    {
        handleEvents();
        update();
        render();
    }
    destroy();
}

void Application::terminate()
{
    mValid = false;
}

bool Application::create()
{
    mWindow = SDL_CreateWindow("ManyNES", 100, 100, 512, 480, SDL_WINDOW_SHOWN);
    if (!mWindow)
        return false;

    mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (!mRenderer)
        return false;

    mScreen = SDL_GetWindowSurface(mWindow);
    if (!mScreen)
        return false;

    mSurface = SDL_CreateRGBSurface(0, 256, 256, 8, 0, 0, 0, 0);
    if (!mSurface)
        return false;

    static const SDL_Color colors[64] =
    {
#include "Palette.h"
    };
    SDL_SetPaletteColors(mSurface->format->palette, colors, 0, 64);

    mRom = NES::Rom::load("mario.nes");
    if (!mRom)
        return false;

    mContext = NES::Context::create(*mRom);
    if (!mContext)
        return false;

    return true;
}

void Application::destroy()
{
    if (mContext)
    {
        mContext->dispose();
        mContext = nullptr;
    }

    if (mRom)
    {
        mRom->dispose();
        mRom = nullptr;
    }

    if (mRenderer)
    {
        SDL_DestroyRenderer(mRenderer);
        mRenderer = nullptr;
    }

    if (mWindow)
    {
        SDL_DestroyWindow(mWindow);
        mWindow = nullptr;
    }

    if (mSurface)
    {
        SDL_FreeSurface(mSurface);
        mSurface = nullptr;
    }
}

void Application::handleEvents()
{
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        if (event.type == SDL_QUIT)
            terminate();

        if (event.type == SDL_KEYDOWN)
        {
            switch (event.key.keysym.sym)
            {
            case SDLK_ESCAPE:
                terminate();
                break;
            }
        }
    }
}

void Application::update()
{
    if (SDL_LockSurface(mSurface) == 0)
    {
        uint8_t* buffer = static_cast<uint8_t*>(mSurface->pixels);
        for (uint32_t y = 0; y < 256; ++y)
        {
            for (uint32_t x = 0; x < 256; ++x)
            {
                *buffer++ = static_cast<uint8_t>(x * y) & 63;
            }
        }

        mContext->update(mSurface->pixels, mSurface->pitch);

        SDL_UnlockSurface(mSurface);
    }
}

void Application::render()
{
    SDL_RenderPresent(mRenderer);

    SDL_Rect rect = { 0, 0, 256, 256 };
    SDL_BlitSurface(mSurface, nullptr, mScreen, &rect);

    SDL_UpdateWindowSurface(mWindow);
}

int main()
{
    Application application;
    application.run();
    return 0;
}
{'fraction_non_alphanumeric': 0.1300739399426588, 'fraction_numerical': 0.017051456164176852, 'mean_word_length': 2.0571955719557193, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7081855', 'n_tokens_mistral': 2347, 'n_tokens_neox': 2161, 'n_words': 450}