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

相关推荐
石像鬼₧魂石2 小时前
内网渗透靶场实操清单(基于 Vulhub+Metasploitable 2)
linux·windows·学习·ubuntu
Danileaf_Guo2 小时前
256台H100服务器算力中心的带外管理网络建设方案
运维·服务器
橘子真甜~3 小时前
C/C++ Linux网络编程15 - 网络层IP协议
linux·网络·c++·网络协议·tcp/ip·计算机网络·网络层
拾贰_C4 小时前
【Linux | Windows | Terminal Command】 Linux---grep | Windows--- findstr
linux·运维·服务器
阿华hhh5 小时前
Linux系统编程(标准io)
linux·开发语言·c++
虹科网络安全5 小时前
艾体宝洞察 | 利用“隐形字符”的钓鱼邮件:传统防御为何失效,AI安全意识培训如何补上最后一道防线
运维·网络·安全
石像鬼₧魂石5 小时前
Kali Linux 网络端口深度扫描
linux·运维·网络
alengan5 小时前
linux上面写python3日志服务器
linux·运维·服务器
yBmZlQzJ6 小时前
免费内网穿透-端口转发配置介绍
运维·经验分享·docker·容器·1024程序员节
JH30736 小时前
docker 新手入门:10分钟搞定基础使用
运维·docker·容器