Mac: “SDL2/SDL_events.h“ file not found解决方案及demo示例

10 篇文章 0 订阅

1 安装SDL2

brew install sdl2

2 创建项目

mkdir myproject
cd myproject
touch sdl_color.c
touch Makefile
mkdir include
mkdir lib

sdl_color.c的源代码为:

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

#include <SDL2/SDL.h>


//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((unsigned int) 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());

    //Initialize 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);
                }

            }
        }

        //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;

}

3 将SDL文件复制到当前项目中

cp -r /usr/local/Cellar/sdl2/2.0.12_1/include myproject/include
cp -r /usr/local/Cellar/sdl2/2.0.12_1/lib myproject/lib

4 创建Makefile文件

game:
	gcc sdl_color.c -o play -I include -L lib -l SDL2-2.0.0

其中:
-I (i as in include) tells it additional include directories you want to add
-L tells it additional library directories you want to add
-l (lowercase l as in lib) tells it specific library binaries you want to add

5 编译

make game

6 运行

./play

结果截图(按’C’随机改变颜色):

在这里插入图片描述
在这里插入图片描述

参考博客
[1] Set up SDL2 on your Mac without Xcode
[2] Install SDL2 on macOS Catalina
[3] Lazy Foo’ Productions

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值