linux sdl图形编程之helloworld.

#include <SDL2/SDL.h>

#include <SDL2/SDL_ttf.h>

int main3(int argc, char* argv\[\]) {

// 初始化SDL

if(SDL_Init(SDL_INIT_VIDEO) < 0) {

printf("SDL初始化失败: %s\n", SDL_GetError());

return -1;

}

// 初始化TTF字体库

if(TTF_Init() < 0) {

printf("TTF初始化失败: %s\n", TTF_GetError());

SDL_Quit();

return -1;

}

// 创建窗口

SDL_Window* window = SDL_CreateWindow("SDL Hello World",

SDL_WINDOWPOS_CENTERED,

SDL_WINDOWPOS_CENTERED,

640, 480,

SDL_WINDOW_SHOWN);

if(!window) {

printf("窗口创建失败: %s\n", SDL_GetError());

TTF_Quit();

SDL_Quit();

return -1;

}

// 创建渲染器

SDL_Renderer* renderer = SDL_CreateRenderer(window, -1,

SDL_RENDERER_ACCELERATED |

SDL_RENDERER_PRESENTVSYNC);

if(!renderer) {

printf("渲染器创建失败: %s\n", SDL_GetError());

SDL_DestroyWindow(window);

TTF_Quit();

SDL_Quit();

return -1;

}

// 加载字体

TTF_Font* font = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 24);

if(!font) {

printf("字体加载失败: %s\n", TTF_GetError());

font = TTF_OpenFont("FreeSans.ttf", 24); // 尝试当前目录

if(!font) {

printf("备用字体加载失败\n");

SDL_DestroyRenderer(renderer);

SDL_DestroyWindow(window);

TTF_Quit();

SDL_Quit();

return -1;

}

}

// 创建文本表面

SDL_Color white = {255, 255, 255, 255};

SDL_Surface* textSurface = TTF_RenderText_Solid(font, "Hello World!", white);

if(!textSurface) {

printf("文本表面创建失败: %s\n", SDL_GetError());

TTF_CloseFont(font);

SDL_DestroyRenderer(renderer);

SDL_DestroyWindow(window);

TTF_Quit();

SDL_Quit();

return -1;

}

// 创建纹理

SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);

if(!textTexture) {

printf("纹理创建失败: %s\n", SDL_GetError());

SDL_FreeSurface(textSurface);

TTF_CloseFont(font);

SDL_DestroyRenderer(renderer);

SDL_DestroyWindow(window);

TTF_Quit();

SDL_Quit();

return -1;

}

// 获取文本尺寸

int textWidth = textSurface->w;

int textHeight = textSurface->h;

SDL_FreeSurface(textSurface);

// 设置文本位置

SDL_Rect textRect = {

(640 - textWidth) / 2,

(480 - textHeight) / 2,

textWidth,

textHeight

};

// 主循环

SDL_Event event;

int quit = 0;

while(!quit) {

while(SDL_PollEvent(&event)) {

if(event.type == SDL_QUIT) {

quit = 1;

}

}

// 清屏

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

SDL_RenderClear(renderer);

// 渲染文本

SDL_RenderCopy(renderer, textTexture, NULL, &textRect);

// 更新屏幕

SDL_RenderPresent(renderer);

}

// 清理资源

SDL_DestroyTexture(textTexture);

TTF_CloseFont(font);

SDL_DestroyRenderer(renderer);

SDL_DestroyWindow(window);

TTF_Quit();

SDL_Quit();

return 0;

}

#include <SDL2/SDL.h>

#include <SDL2/SDL_ttf.h>

int main4() {

// 初始化

if(SDL_Init(SDL_INIT_VIDEO) < 0) {

SDL_Log("SDL初始化失败: %s", SDL_GetError());

return -1;

}

if(TTF_Init() < 0) {

SDL_Log("TTF初始化失败: %s", TTF_GetError());

SDL_Quit();

return -1;

}

// 创建窗口和主Surface

SDL_Window* window = SDL_CreateWindow("SDL Surface Demo",

SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,

640, 480, SDL_WINDOW_SHOWN);

if(!window) {

SDL_Log("窗口创建失败: %s", SDL_GetError());

TTF_Quit();

SDL_Quit();

return -1;

}

SDL_Surface* screenSurface = SDL_GetWindowSurface(window);

// 加载字体和渲染文本

TTF_Font* font = TTF_OpenFont("FreeSans.ttf", 24);

if(!font) font = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 24);

if(font) {

SDL_Color white = {255, 255, 255};

SDL_Surface* textSurface = TTF_RenderText_Solid(font, "Hello World!", white);

if(textSurface) {

// 计算居中位置

SDL_Rect textRect = {

(screenSurface->w - textSurface->w) / 2,

(screenSurface->h - textSurface->h) / 2,

textSurface->w,

textSurface->h

};

// 主循环

SDL_Event e;

int running = 1;

while(running) {

while(SDL_PollEvent(&e)) {

if(e.type == SDL_QUIT) running = 0;

}

// 清屏(填充黑色)

SDL_FillRect(screenSurface, NULL,

SDL_MapRGB(screenSurface->format, 0, 0, 0));

// 绘制文本

SDL_BlitSurface(textSurface, NULL, screenSurface, &textRect);

SDL_UpdateWindowSurface(window);

SDL_Delay(16); // 约60FPS

}

SDL_FreeSurface(textSurface);

}

TTF_CloseFont(font);

}

// 清理资源

SDL_DestroyWindow(window);

TTF_Quit();

SDL_Quit();

return 0;

}

#include <SDL2/SDL.h>

#include <SDL2/SDL_ttf.h>

#define SCREEN_WIDTH 640

#define SCREEN_HEIGHT 480

typedef struct {

SDL_Rect rect;

SDL_Color normalColor;

SDL_Color hoverColor;

SDL_Color pressedColor;

int isPressed;

int isHovered;

} Button;

void renderButton(SDL_Renderer* renderer, Button* button) {

SDL_Color currentColor = button->normalColor;

if (button->isPressed) {

currentColor = button->pressedColor;

} else if (button->isHovered) {

currentColor = button->hoverColor;

}

SDL_SetRenderDrawColor(renderer, currentColor.r, currentColor.g, currentColor.b, 255);

SDL_RenderFillRect(renderer, &button->rect);

// 绘制边框

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

SDL_RenderDrawRect(renderer, &button->rect);

}

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

SDL_Init(SDL_INIT_VIDEO);

TTF_Init();

SDL_Window* window = SDL_CreateWindow("SDL Button Demo",

SDL_WINDOWPOS_CENTERED,

SDL_WINDOWPOS_CENTERED,

SCREEN_WIDTH, SCREEN_HEIGHT,

SDL_WINDOW_SHOWN);

SDL_Renderer* renderer = SDL_CreateRenderer(window, -1,

SDL_RENDERER_ACCELERATED);

// 创建按钮

Button myButton = {

.rect = {SCREEN_WIDTH/2 - 100, SCREEN_HEIGHT/2 - 50, 200, 100},

.normalColor = {100, 100, 200, 255},

.hoverColor = {150, 150, 250, 255},

.pressedColor = {50, 50, 150, 255},

.isPressed = 0,

.isHovered = 0

};

// 主循环

SDL_Event event;

int running = 1;

while (running) {

// 处理事件

while (SDL_PollEvent(&event)) {

switch (event.type) {

case SDL_QUIT:

running = 0;

break;

case SDL_MOUSEMOTION:

// 检测鼠标悬停

myButton.isHovered = SDL_PointInRect(

&(SDL_Point){event.motion.x, event.motion.y},

&myButton.rect

);

break;

case SDL_MOUSEBUTTONDOWN:

if (event.button.button == SDL_BUTTON_LEFT &&

SDL_PointInRect(&(SDL_Point){event.button.x, event.button.y},

&myButton.rect)) {

myButton.isPressed = 1;

}

break;

case SDL_MOUSEBUTTONUP:

if (event.button.button == SDL_BUTTON_LEFT) {

if (myButton.isPressed &&

SDL_PointInRect(&(SDL_Point){event.button.x, event.button.y},

&myButton.rect)) {

SDL_Log("Button clicked!");

}

myButton.isPressed = 0;

}

break;

}

}

// 渲染

SDL_SetRenderDrawColor(renderer, 240, 240, 240, 255);

SDL_RenderClear(renderer);

renderButton(renderer, &myButton);

SDL_RenderPresent(renderer);

SDL_Delay(16);

}

// 清理资源

SDL_DestroyRenderer(renderer);

SDL_DestroyWindow(window);

TTF_Quit();

SDL_Quit();

return 0;

}

gcc main.c -o helloworld `sdl2-config --cflags --libs` -lSDL2_ttf -lcapstone

相关推荐
大树887 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠7 小时前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质7 小时前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush47 小时前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5207 小时前
Linux 11 动态监控指令top
linux
Inhand陈工8 小时前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智8 小时前
ARP代理--工作原理
运维·网络·arp·arp代理
不会C语言的男孩9 小时前
Linux 系统编程 · 第 8 章:进程基础
linux·c语言
shushangyun_9 小时前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
古城小栈9 小时前
Unix 与 Linux 异同小叙
linux·服务器·unix