-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.cpp
148 lines (134 loc) · 3.09 KB
/
form.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "form.hpp"
#include "geometry.hpp"
#define PI 3.1415926535
#include <cmath>
#include <cstdio>
form::form(SDL_Renderer* pRenderer)
:gRenderer(pRenderer)
{
resourceST=0;
hasDraw=false;
Rot=0;
Pos=point(320,240);
if(Load()!=0)
return ;
obj2.img=obj.img=img;
obj2.clip=obj.clip={0,0,214,189};
obj2.center=obj.center={104,80};
//設定矩形
geometry_base *pGeo;
pGeo=new rect;
dynamic_cast<rect*>(pGeo)->setRect(-104,-75,70,70);
obj.shape.push_back(pGeo);
pGeo=new rect;
dynamic_cast<rect*>(pGeo)->setRect(-104,-75,70,70);
obj2.shape.push_back(pGeo);
//設定圓形
pGeo=new circle;
dynamic_cast<circle*>(pGeo)->center=point(69.282,-40);
dynamic_cast<circle*>(pGeo)->radius=40;
obj.shape.push_back(pGeo);
pGeo=new circle;
dynamic_cast<circle*>(pGeo)->center=point(69.282,-40);
dynamic_cast<circle*>(pGeo)->radius=40;
obj2.shape.push_back(pGeo);
//設定三角形
pGeo=new polygen;
dynamic_cast<polygen*>(pGeo)->points.push_back(point(0,22.265));
dynamic_cast<polygen*>(pGeo)->points.push_back(point(-50,108.866));
dynamic_cast<polygen*>(pGeo)->points.push_back(point(50,108.866));
obj.shape.push_back(pGeo);
pGeo=new polygen;
dynamic_cast<polygen*>(pGeo)->points.push_back(point(0,22.265));
dynamic_cast<polygen*>(pGeo)->points.push_back(point(-50,108.866));
dynamic_cast<polygen*>(pGeo)->points.push_back(point(50,108.866));
obj2.shape.push_back(pGeo);
}
form::~form()
{
for(int i=obj.shape.size()-1;i>=0;--i)
delete obj.shape[i];
for(int i=obj2.shape.size()-1;i>=0;--i)
delete obj2.shape[i];
Close();
}
int form::eventHandle()
{
int ret=1;
SDL_Event evt;
while(SDL_PollEvent(&evt)!= 0)
{
switch(evt.type)
{
case SDL_QUIT:
ret=0;
break;
case SDL_MOUSEMOTION:
//showStatus(evt.button.x,evt.button.y);
Pos=point(evt.motion.x,evt.motion.y);
break;
case SDL_MOUSEWHEEL:
Rot+=evt.wheel.y*5;
break;
}
}
draw();
return ret;
}
void form::draw()
{
/*if(hasDraw)
return;
hasDraw=true;*/
int i,j,N,cx,cy,rot;
SDL_GetRendererOutputSize(gRenderer,&cx,&cy);
if(isReady())
{
//SDL_RenderCopy(gRenderer,img,NULL,NULL);
if(obj.isCollise(obj2,Pos,Rot,point(320,240),30))
SDL_SetRenderDrawColor(gRenderer, 200, 200, 200, 255);
else
SDL_SetRenderDrawColor(gRenderer, 255, 255, 255, 255);
SDL_RenderClear(gRenderer);
obj2.draw(gRenderer,point(320,240),30);
obj.draw(gRenderer,Pos,Rot);
}
else
printf("not ready");
SDL_RenderPresent(gRenderer);
}
void form::showStatus(int mx,int my)
{
/*point src(mx,my),dest;
poly.closePoint(src,dest,point(320,240),Rot);
printf("(%d,%d)-(%.1lf,%.1lf)\n",mx,my,dest.x,dest.y);*/
}
int form::Load()
{
SDL_Surface *suf=SDL_LoadBMP("img2.bmp");
if(suf==NULL)
{
printf("\nERR:%s\n",SDL_GetError());
Close();
return FORM_IMG;
}
SDL_SetColorKey(suf,SDL_TRUE,SDL_MapRGB(suf->format,0xFF,0x00,0xFF));
img=SDL_CreateTextureFromSurface(gRenderer,suf);
resourceST=FORM_IMG;
SDL_FreeSurface(suf);
return 0;
}
void form::Close()
{
switch(resourceST)
{
case FORM_IMG:
SDL_DestroyTexture(img);
default:
resourceST=0;
}
}
bool form::isReady()
{
return resourceST==FORM_IMG;
}