Skip to content
Andrey Ch edited this page Sep 17, 2024 · 2 revisions

Welcome to the tcc-source-c wiki!

Пример запуска SDL2, SDL на tcc:

сделать patch:

fix

рабочий пример: Как компилировать SDL2, SDL на tcc

рабочий пример:

_make_tcc.bat

rem -DSDL_DISABLE_IMMINTRIN_H=1
rem D:\bin\tcc\tcc.exe -v -ID:/bin/tcc/include -lSDL sdl01.c
D:\bin\tcc\tcc.exe -v -ID:/bin/tcc/include -lSDL2 hello2.c



hello2.c

#include <stdio.h>
#include <stdbool.h>
#include <time.h>


//https://github.com/libsdl-org/SDL/commit/9216b7a5eed3a55dc6c0bdccbeabc9183a701b16

#define SDL_MAIN_HANDLED
#define SDL_DISABLE_IMMINTRIN_H


#include <SDL2/SDL.h>



// Normally SDL2 will redefine the main entry point of the program for Windows applications
// this doesn't seem to play nice with TCC, so we just undefine the redefinition
#ifdef __TINYC__
    #undef main
#endif



// Utility macros
#define CHECK_ERROR(test, message) \
    do { \
        if((test)) { \
            fprintf(stderr, "%s\n", (message)); \
            exit(1); \
        } \
    } while(0)

// Get a random number from 0 to 255
int randInt(int rmin, int rmax) {
    return rand() % rmax + rmin;
}
    
// Window dimensions
static const int width = 800;
static const int height = 600;

int main(int argc, char **argv) {
    // Initialize the random number generator
    srand(time(NULL));
    
    // Initialize SDL
    CHECK_ERROR(SDL_Init(SDL_INIT_VIDEO) != 0, SDL_GetError());

    // Create an SDL window
    SDL_Window *window = SDL_CreateWindow("Hello, SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
    CHECK_ERROR(window == NULL, SDL_GetError());

    // Create a renderer (accelerated and in sync with the display refresh rate)
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);    
    CHECK_ERROR(renderer == NULL, SDL_GetError());

    // Initial renderer color
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);

    bool running = true;
    SDL_Event event;
    while(running) {
        // Process events
        while(SDL_PollEvent(&event)) {
            if(event.type == SDL_QUIT) {
                running = false;
            } else if(event.type == SDL_KEYDOWN) {
                const char *key = SDL_GetKeyName(event.key.keysym.sym);
                if(strcmp(key, "C") == 0) {
                    SDL_SetRenderDrawColor(renderer, randInt(0, 255), randInt(0, 255), randInt(0, 255), 255);
                } else if(strcmp(key, "Q") == 0) {
                    running = false;
                }                    
            }
        }

        // Clear screen
        SDL_RenderClear(renderer);

        // Draw

        // Show what was drawn
        SDL_RenderPresent(renderer);
    }

    // Release resources
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}



sdl01.c



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


// Normally SDL2 will redefine the main entry point of the program for Windows applications
// this doesn't seem to play nice with TCC, so we just undefine the redefinition
#ifdef __TINYC__
    #undef main
#endif




const int WINDOW_WIDTH = 1024;
const int WINDOW_HEIGHT = 720;

//int main(int argc, char** argv)
int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Surface* screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == 0) {
        printf("! Error SDL_SetVideoMode \n");
    	exit(EXIT_FAILURE);			
    }

    SDL_WM_SetCaption(" A Simpke SDL Windows", 0);

    SDL_Event event;
    bool quit = false;

    while(!quit)
    {
        while ( SDL_PollEvent(&event) )
        {
           switch ( event.type )
           {
             case SDL_QUIT : quit = true; break;

             case SDL_KEYDOWN:
             {
             	switch( event.key.keysym.sym )
             	{
             		case SDLK_ESCAPE : quit = true; break;
             		default : break;
             	}
             }

             default : break;
           }
        }

    }
		

//        Uint32 now = SDL_GetTicks();
//        SDL_GL_SwapBuffers();
//
//        int delay = 1000 / 60 - (SDL_GetTicks() - now);
//        if(delay > 0) SDL_Delay(delay);

  //Quit SDL
    SDL_Quit();
    return EXIT_SUCCESS;
}

Пример, настройки плугина Npp-Exec для notepad++

echo > Setup required Environment
echo -------------------------------------
ENV_SET PATH=C:\tcc 
cd $(CURRENT_DIRECTORY)
echo > Saving Current File
echo -------------------------
npp_save
echo
echo > run program
echo -----------------------
cmd /c tcc -vvv -lglfw -lopengl32 -lglu32 -DGLFW_DLL -run $(FILE_NAME)
echo
echo > Reset Environment
echo --------------------------
ENV_UNSET PATH

Clone this wiki locally