类的问题
sdl吧
全部回复
仅看楼主
level 6
akpkpkp654 楼主
#include <iostream>
#include <SDL.h>
#include<SDL_ttf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <codecvt>
#include<SDL_image.h>
//#include "font.h"
/*
* Lesson 0: Test to make sure SDL is setup properly
*/
//SDL_Window *win;
//SDL_Renderer *ren;
//SDL_Surface *sur;
//TTF_SizeUNICODE(TTF_Font *font, const Unit16 *text, int *w, int *h);
//sur=SDL_CreateRGBSurface(0, 100, 100, 32,0, 0, 0, 0);
class button
{
public:
// button();
~button();
SDL_Surface *sur;
int w, h, bw, bh, mx, my,lx1 = NULL;
SDL_Surface *TTF = NULL;
SDL_Rect rect;
SDL_Rect re;
SDL_Surface* loadedSurface=NULL;
// typedef void (*hs)();
void Set(int w, int h,int lx);
SDL_Surface *xs(SDL_Surface *sur1, int x,int y);
void SetText(char *text, int size, SDL_Color fontcolor, char *path);
bool ButtonDown(SDL_Event *e);
bool ButtonUp(SDL_Event *e);
void button::ButtonTp(char *path);
};
button::~button()
{
TTF_Quit();
IMG_Quit();
}
void button::SetText(char *text, int size, SDL_Color fontcolor ,char *path)
{
if (TTF_Init() == -1) SDL_Quit();
TTF_Font *font;
font = TTF_OpenFont(path, size);
TTF = TTF_RenderText_Blended(font, text, fontcolor);
TTF_SizeText(font, text, &w, &h);
};
void button::Set(int bw, int bh,int lx)
{
lx1 = lx;
this->bw = bw;
this->bh = bh;
sur = SDL_CreateRGBSurface(0, bw, bh, 32, 0, 0xffffffff, 0, 255);
if (sur == nullptr)
{
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
}
SDL_FillRect(sur, NULL, 0x000ff0ff);
};
void button::ButtonTp(char *path )
{
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
}
loadedSurface = IMG_Load(path);
if (loadedSurface == NULL)
{
printf("Unable to load image %s! SDL_image Error: %s\n", path, IMG_GetError());
}
}
SDL_Surface *button::xs(SDL_Surface *sur1 , int x,int y)
{
re = { x, y };
rect.x = (bw / 2) - (w / 2);
rect.y = (bh / 2) - (h / 2);
rect.w = w;
rect.h = h;
if (lx1 == 0)
{
SDL_BlitSurface(TTF, nullptr, sur, &rect);
SDL_BlitSurface(sur, NULL, sur1, &re);
}
if (lx1 == 1)
{
SDL_Rect u;
u.x = 0;
u.y = 0;
u.w = loadedSurface->w / 3;
u.h = loadedSurface->h;
SDL_BlitSurface(loadedSurface, &u, sur1, &re);
}
if (lx1 == 2)
{
}
SDL_FreeSurface(TTF);
SDL_FreeSurface(sur);
return sur1;
}
bool button::ButtonDown(SDL_Event *e)
{
bool down = false;
//If mouse event happened
if (e->type == SDL_MOUSEBUTTONDOWN )
//e->type == SDL_MOUSEMOTION || e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEBUTTONUP
{
//Get mouse position
SDL_GetMouseState(&mx, &my);
if (re.x < mx &&mx< bw)
{
if (re.y < my &&my< bh)
{
down = true;
}
}
}
return down;
}
bool button::ButtonUp(SDL_Event *e)
{
bool up = false;
if ( e->type == SDL_MOUSEBUTTONUP)
{
//Get mouse position
SDL_GetMouseState(&mx, &my);
//Mouse is left of the button
if (re.x < mx&&mx < bw)
{
if (re.y < my&&my < bh)
{
//SDL_Quit();
up = true;
}
}
}
return up;
}
int main(int argc, char** argv)
{
button but,but1;
//int *quit = 0;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
//Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it
SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_BORDERLESS );// SDL_WINDOW_SHOWN ||
//Make sure creating our window went ok
if (win == nullptr){
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
return 1;
}
//Create a renderer that will draw to the window, -1 specifies that we want to load whichever
//video driver supports the flags we're passing
//Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering
//SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be
//synchornized with the monitor's refresh rate
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == nullptr){
SDL_DestroyWindow(win);
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_Surface *sur = SDL_CreateRGBSurface(0, 640, 480, 32,0, 0, 0,0);
if (sur == NULL)
{
fprintf(stderr, "CreateRGBSurface failed: %s\n", SDL_GetError());
exit(1);
}
SDL_FillRect (sur, NULL, 0xffffffff);
SDL_Color color = { 255, 0, 0, 0 };
but.Set(100, 50,NULL);
//but.ButtonTp("close.png");
but.SetText("hello", 23, color, "DroidSans.ttf");
but.xs(sur,10,10);
but1.Set(200, 50,1);
//but1.SetText("wang", 30, {255,0,255}, "DroidSans.ttf");
but1.ButtonTp("close.png");
int p=but1.loadedSurface->w/3;
int k=but1.loadedSurface->h;
but1.xs(sur,(640-p),0);
//SDL_Rect re = { 10, 10, };
//SDL_BlitSurface(sur1,NULL,sur,&re);
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, sur);
SDL_FreeSurface(sur);
SDL_RenderClear(ren);
//Draw the texture
SDL_RenderCopy(ren, tex, NULL, NULL);
//Event handler
bool quit = false;
SDL_Event e;
while (!quit)
{
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
quit =true;
}
if (but1.ButtonDown(&e) == true)
{
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
if (but.ButtonUp(&e) == true)
{
quit = true;
}
if (but1.ButtonDown(&e) == true)
{
quit=true;
}
//but.ButtonUp( &e);
}
SDL_RenderPresent(ren);
}
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
为什么button的对象but有事件响应,对象but1没有
2014年12月21日 03点12分 1
level 6
akpkpkp654 楼主
快点啊,人啊[疑问]
2014年12月21日 03点12分 2
level 6
akpkpkp654 楼主
在线等[]
2014年12月21日 03点12分 3
level 8
那么长谁看。
2014年12月21日 04点12分 4
这个吧人好少
2014年12月21日 05点12分
电脑上试一下[泪]
2014年12月21日 05点12分
level 6
akpkpkp654 楼主
人啊,
2014年12月21日 10点12分 5
1