用c语言写一个美观的轮播图,功能性要强
时间: 2024-05-01 12:16:46 浏览: 70
实用的轮播图
由于轮播图需要图形界面支持,因此需要使用相应的图形库。以下是使用SDL2图形库实现的一个简单的美观的轮播图示例代码:
```c
#include <stdio.h>
#include <SDL2/SDL.h>
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define IMAGE_WIDTH 400
#define IMAGE_HEIGHT 300
#define IMAGE_X (WINDOW_WIDTH - IMAGE_WIDTH) / 2
#define IMAGE_Y (WINDOW_HEIGHT - IMAGE_HEIGHT) / 2
int main(int argc, char* argv[])
{
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* texture1 = NULL;
SDL_Texture* texture2 = NULL;
SDL_Event event;
int quit = 0;
int current_image = 0;
int next_image = 1;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("轮播图", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_Surface* surface1 = SDL_LoadBMP("image1.bmp");
texture1 = SDL_CreateTextureFromSurface(renderer, surface1);
SDL_FreeSurface(surface1);
SDL_Surface* surface2 = SDL_LoadBMP("image2.bmp");
texture2 = SDL_CreateTextureFromSurface(renderer, surface2);
SDL_FreeSurface(surface2);
while (!quit)
{
while (SDL_PollEvent(&event) != 0)
{
if (event.type == SDL_QUIT)
{
quit = 1;
}
}
SDL_RenderClear(renderer);
if (current_image == 0)
{
SDL_RenderCopy(renderer, texture1, NULL, &(SDL_Rect){ .x = IMAGE_X, .y = IMAGE_Y, .w = IMAGE_WIDTH, .h = IMAGE_HEIGHT });
}
else
{
SDL_RenderCopy(renderer, texture2, NULL, &(SDL_Rect){ .x = IMAGE_X, .y = IMAGE_Y, .w = IMAGE_WIDTH, .h = IMAGE_HEIGHT });
}
SDL_RenderPresent(renderer);
SDL_Delay(2000);
current_image = next_image;
next_image = (next_image + 1) % 2;
}
SDL_DestroyTexture(texture1);
SDL_DestroyTexture(texture2);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
```
该程序使用SDL2图形库创建了一个窗口,并在窗口中显示了两张图片,每隔2秒切换一次图片。可以通过添加更多的图片和更复杂的切换动画实现更美观、更具功能性的轮播图。
阅读全文