梦幻西游

define _CRT_SECURE_NO_WARNINGS

include<stdio.h>

include<SDL.h>

include<SDL_ttf.h>

include<SDL_image.h>

include

using namespace std;

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

if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "SDL环境初始化失败" << SDL_GetError() << endl;
}
TTF_Init();
cout << "SDL环境初始化成功" << endl;

//创建一个游戏窗口
SDL_Window* gamewin = SDL_CreateWindow("MyGame", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
if (!gamewin)
{
cout << "游戏窗口创建失败" << SDL_GetError() << endl;
return -1;
}
cout << "游戏窗口创建成功" << endl;

SDL_Renderer* renderer = SDL_CreateRenderer(gamewin, -1, SDL_RENDERER_ACCELERATED);
if (!renderer)
{
cout << "游戏渲染器创建失败" << SDL_GetError() << endl;
SDL_DestroyWindow(gamewin);
SDL_Quit();
return -1;
}

TTF_Font* font = TTF_OpenFont("arial.ttf", 32);
if (!font)
{
cout << "字体无法加载" << SDL_GetError() << endl;
SDL_DestroyWindow(gamewin);
SDL_Quit();
return -1;
}

//创建图像

//图像1 登录界面
SDL_Texture* loginImage = IMG_LoadTexture(renderer, "login.jpg");
if (!loginImage)
{
cout << "图像无法加载" << SDL_GetError() << endl;
SDL_DestroyWindow(gamewin);
SDL_Quit();
return -1;
}
//把图像拷贝到渲染器里 原尺寸 null 现尺寸 定义
SDL_Rect destRect = { 0,0,640,480 };

SDL_RenderCopy(renderer, loginImage, NULL, &destRect);

//图像2 登录按钮
SDL_Texture* loginBtnImage = IMG_LoadTexture(renderer, "img/loginButton.png");
if (!loginBtnImage)
{
cout << "图像无法加载" << SDL_GetError() << endl;
SDL_DestroyWindow(gamewin);
SDL_Quit();
return -1;
}
//把图像拷贝到渲染器里 原尺寸 null 现尺寸 定义
SDL_Rect dest1Rect = { 220,300,210,71 };

SDL_RenderCopy(renderer, loginBtnImage, NULL, &dest1Rect);

//-------------------------------------------

SDL_Texture* attack[10];
char attackname[32] = { 0 };

SDL_Rect attacksrcRect = { 0,0,71,110 };//原始
SDL_Rect attackdestRect = { 100,100,71,110 };//目标

for (int i = 0; i < 10; i++)
{
memset(attackname, 0, sizeof attackname);
sprintf(attackname, "img//attack//attack_%d.png", i);
attack[i] = IMG_LoadTexture(renderer, attackname);
}

//-------------------------------------------

SDL_Color textcolor = { 255,255,255 };

//字体 文本 颜色
SDL_Surface* surfacemessage = TTF_RenderText_Solid(font, "Hello World", textcolor);

//数据加载
SDL_Texture* message = SDL_CreateTextureFromSurface(renderer, surfacemessage);

SDL_Rect rect = { 100,100,surfacemessage->w,surfacemessage->h };

SDL_RenderCopy(renderer, message, NULL, &rect);

//显示
SDL_RenderPresent(renderer);

//事件
SDL_Event event;
bool quit = false;
int attacknum = 0;
int isLogin = 0;
while (!quit) {

SDL_Delay(100);

while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
quit = true;
}
if (event.type == SDL_KEYDOWN) {
printf("按下了 下方向\n");
}
if (event.type == SDL_MOUSEBUTTONDOWN)
{
int xpos = event.button.x;
int ypos = event.button.y;
printf("鼠标按下了 (x=%d, y=%d)\n", xpos, ypos);

if (xpos > dest1Rect.x && dest1Rect.x + dest1Rect.w && ypos > dest1Rect.y && ypos < dest1Rect.y + dest1Rect.w)
{
printf("点到了登录按钮\n");
isLogin = 1;

//释放原有窗口
SDL_DestroyTexture(loginImage);

}
}
}
if (isLogin == 1)
{
SDL_Texture* gameImage = IMG_LoadTexture(renderer, "img/warBg.jpg");
if (!gameImage)
{
cout << "图像无法加载" << SDL_GetError() << endl;
SDL_DestroyWindow(gamewin);
SDL_Quit();
return -1;
}

//把图像拷贝到渲染器里 原尺寸 null 现尺寸 定义
SDL_Rect destRect = { 0,0,640,480 };

SDL_RenderCopy(renderer, gameImage, NULL, &destRect);

SDL_RenderCopy(renderer, attack[attacknum++], &attacksrcRect, &attackdestRect);
if (attacknum == 10)attacknum = 0;
}

SDL_RenderPresent(renderer);
}

system("pause");
return 0;
}

posted @ 2025-03-13 14:55  某朝  阅读(34)  评论(0)    收藏  举报