Skip to content

Commit 923e41f

Browse files
authored
Update main.c
1 parent 77f3146 commit 923e41f

File tree

1 file changed

+189
-32
lines changed

1 file changed

+189
-32
lines changed

main.c

+189-32
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const unsigned char icon[] = { // 16, 16, 4
8787
// globals
8888
//*************************************
8989
const char appTitle[] = "Voxel Paint";
90-
const char appVersion[] = "v2.2";
90+
const char appVersion[] = "v2.3";
9191
char *basedir, *appdir;
9292
SDL_Window* wnd;
9393
SDL_GLContext glc;
@@ -96,6 +96,8 @@ int winw = 1024, winh = 768;
9696
int winw2 = 512, winh2 = 384;
9797
float ww, wh;
9898
float aspect, t = 0.f;
99+
uint drag=0,size=0,dsx=0,dsy=0;
100+
int mx=0, my=0, xd=0, yd=0, md=0;
99101
uint g_fps = 0;
100102
uint ks[10] = {0}; // keystate
101103
uint focus_mouse = 0; // mouse lock
@@ -979,6 +981,26 @@ int lenText(const char* s)
979981
//*************************************
980982
// update & render
981983
//*************************************
984+
void WOX_QUIT()
985+
{
986+
SDL_HideWindow(wnd);
987+
saveState("");
988+
drawText(NULL, "*K", 0, 0, 0);
989+
SDL_FreeSurface(s_icon);
990+
SDL_FreeSurface(sHud);
991+
SDL_GL_DeleteContext(glc);
992+
SDL_DestroyWindow(wnd);
993+
SDL_Quit();
994+
exit(0);
995+
}
996+
void WOX_POP(const int w, const int h)
997+
{
998+
winw = w;
999+
winh = h;
1000+
winw2 = winw/2;
1001+
winh2 = winh/2;
1002+
doPerspective();
1003+
}
9821004
void main_loop()
9831005
{
9841006
//*************************************
@@ -992,10 +1014,9 @@ void main_loop()
9921014
//*************************************
9931015
// input handling
9941016
//*************************************
995-
static int mx=0, my=0, lx=0, ly=0, md=0;
996-
static float idle = 0.f;
997-
1017+
9981018
// if user is idle for 3 minutes, save.
1019+
static float idle = 0.f;
9991020
if(idle != 0.f && t-idle > 180.f)
10001021
{
10011022
// char tmp[32];
@@ -1012,6 +1033,45 @@ void main_loop()
10121033
// on input a new idle is set, and a
10131034
// count-down for a new save begins.
10141035
}
1036+
1037+
// window decor stuff
1038+
if(drag == 1)
1039+
{
1040+
static int lx=0, ly=0;
1041+
static float lt = 0;
1042+
if(t > lt)
1043+
{
1044+
if(lx != mx || ly != my)
1045+
{
1046+
int x,y;
1047+
SDL_GetWindowPosition(wnd, &x, &y);
1048+
SDL_SetWindowPosition(wnd, x+(mx-dsx), y+(my-dsy));
1049+
lx = mx, ly = my;
1050+
}
1051+
lt = t+0.03f;
1052+
}
1053+
}
1054+
if(size == 1)
1055+
{
1056+
static float lt = 0;
1057+
if(t > lt)
1058+
{
1059+
int w,h;
1060+
SDL_GetWindowSize(wnd, &w, &h);
1061+
winw = w+(mx-dsx);
1062+
winh = h+(my-dsy);
1063+
dsx = mx;
1064+
dsy = my;
1065+
if(winw > 380 && winh > 380)
1066+
{
1067+
SDL_SetWindowSize(wnd, winw, winh);
1068+
winw2 = winw/2;
1069+
winh2 = winh/2;
1070+
doPerspective();
1071+
}
1072+
lt = t+0.03f;
1073+
}
1074+
}
10151075

10161076
SDL_Event event;
10171077
while(SDL_PollEvent(&event))
@@ -1020,6 +1080,21 @@ void main_loop()
10201080
{
10211081
case SDL_KEYDOWN:
10221082
{
1083+
if(event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_TAB) // unlock mouse focus
1084+
{
1085+
focus_mouse = 1 - focus_mouse;
1086+
SDL_ShowCursor(1 - focus_mouse);
1087+
if(focus_mouse == 1)
1088+
{
1089+
SDL_GetRelativeMouseState(&xd, &yd);
1090+
SDL_SetRelativeMouseMode(SDL_TRUE);
1091+
}
1092+
else
1093+
{
1094+
SDL_GetRelativeMouseState(&xd, &yd);
1095+
SDL_SetRelativeMouseMode(SDL_FALSE);
1096+
}
1097+
}
10231098
if(focus_mouse == 0){break;}
10241099
if(event.key.keysym.sym == SDLK_w){ks[0] = 1;}
10251100
else if(event.key.keysym.sym == SDLK_a){ks[1] = 1;}
@@ -1031,11 +1106,6 @@ void main_loop()
10311106
else if(event.key.keysym.sym == SDLK_UP){ks[7] = 1;}
10321107
else if(event.key.keysym.sym == SDLK_DOWN){ks[8] = 1;}
10331108
else if(event.key.keysym.sym == SDLK_SPACE){ks[9] = 1;} // move up Z
1034-
else if(event.key.keysym.sym == SDLK_ESCAPE) // unlock mouse focus
1035-
{
1036-
focus_mouse = 0;
1037-
SDL_ShowCursor(1);
1038-
}
10391109
else if(event.key.keysym.sym == SDLK_q) // clone pointed voxel texture
10401110
{
10411111
if(ise == 0)
@@ -1434,9 +1504,9 @@ void main_loop()
14341504

14351505
case SDL_MOUSEMOTION:
14361506
{
1437-
if(focus_mouse == 0){break;}
14381507
mx = event.motion.x;
14391508
my = event.motion.y;
1509+
if(focus_mouse == 0){break;}
14401510

14411511
if(ise == 1)
14421512
{
@@ -1471,7 +1541,18 @@ void main_loop()
14711541

14721542
case SDL_MOUSEBUTTONUP:
14731543
{
1474-
if(event.button.button == SDL_BUTTON_LEFT){ptt = 0.f;}
1544+
if(event.button.button == SDL_BUTTON_LEFT)
1545+
{
1546+
if(drag == 1 || size == 1)
1547+
{
1548+
drag=0;
1549+
size=0;
1550+
SDL_GetWindowSize(wnd, &winw, &winh);
1551+
WOX_POP(winw, winh);
1552+
SDL_CaptureMouse(SDL_FALSE);
1553+
}
1554+
ptt = 0.f;
1555+
}
14751556
else if(event.button.button == SDL_BUTTON_RIGHT){dtt = 0.f;}
14761557
else if(event.button.button == SDL_BUTTON_MIDDLE) // clone pointed voxel
14771558
{
@@ -1508,15 +1589,82 @@ void main_loop()
15081589

15091590
case SDL_MOUSEBUTTONDOWN:
15101591
{
1511-
lx = event.button.x;
1512-
ly = event.button.y;
15131592
mx = event.button.x;
15141593
my = event.button.y;
15151594

1595+
static float llct = 0.f;
1596+
static uint maxed = 0;
1597+
1598+
if(event.button.button == SDL_BUTTON_LEFT) // check window decor stuff
1599+
{
1600+
if(focus_mouse == 0)
1601+
{
1602+
if(llct != 0.f && t-llct < 0.3f)
1603+
{
1604+
if(maxed == 0)
1605+
{
1606+
SDL_MaximizeWindow(wnd);
1607+
maxed = 1;
1608+
drag = 0;
1609+
size = 0;
1610+
llct = t;
1611+
break;
1612+
}
1613+
else
1614+
{
1615+
SDL_RestoreWindow(wnd);
1616+
maxed = 0;
1617+
drag = 0;
1618+
size = 0;
1619+
llct = t;
1620+
break;
1621+
}
1622+
}
1623+
llct = t;
1624+
if(my < 22)
1625+
{
1626+
if(mx < 14)
1627+
{
1628+
WOX_QUIT();
1629+
break;
1630+
}
1631+
else if(mx < 28)
1632+
{
1633+
SDL_MinimizeWindow(wnd);
1634+
break;
1635+
}
1636+
else if(mx > winw-14)
1637+
{
1638+
WOX_QUIT();
1639+
break;
1640+
}
1641+
else if(mx > winw-28)
1642+
{
1643+
SDL_MinimizeWindow(wnd);
1644+
break;
1645+
}
1646+
1647+
dsx = mx, dsy = my;
1648+
drag=1;
1649+
SDL_CaptureMouse(SDL_TRUE);
1650+
break;
1651+
}
1652+
else if(mx > winw-15 && my > winh-15)
1653+
{
1654+
size = 1;
1655+
dsx = mx, dsy = my;
1656+
SDL_CaptureMouse(SDL_TRUE);
1657+
break;
1658+
}
1659+
}
1660+
}
1661+
15161662
if(focus_mouse == 0) // lock mouse focus on every mouse input to the window
15171663
{
15181664
SDL_ShowCursor(0);
15191665
focus_mouse = 1;
1666+
SDL_GetRelativeMouseState(&xd, &yd);
1667+
SDL_SetRelativeMouseMode(SDL_TRUE);
15201668
break;
15211669
}
15221670

@@ -1563,14 +1711,7 @@ void main_loop()
15631711

15641712
case SDL_QUIT:
15651713
{
1566-
SDL_HideWindow(wnd);
1567-
saveState("");
1568-
SDL_FreeSurface(s_icon);
1569-
SDL_FreeSurface(sHud);
1570-
SDL_GL_DeleteContext(glc);
1571-
SDL_DestroyWindow(wnd);
1572-
SDL_Quit();
1573-
exit(0);
1714+
WOX_QUIT();
15741715
}
15751716
break;
15761717
}
@@ -1709,12 +1850,11 @@ void main_loop()
17091850
//*************************************
17101851
// camera/mouse control
17111852
//*************************************
1712-
const float xd = lx-mx;
1713-
const float yd = ly-my;
1853+
SDL_GetRelativeMouseState(&xd, &yd);
17141854
if(xd != 0 || yd != 0)
17151855
{
1716-
g.xrot += xd*g.sens;
1717-
g.yrot += yd*g.sens;
1856+
g.xrot -= xd*g.sens;
1857+
g.yrot -= yd*g.sens;
17181858

17191859
if(g.plock == 1)
17201860
{
@@ -1730,9 +1870,6 @@ void main_loop()
17301870
if(g.yrot < 0.1f)
17311871
g.yrot = 0.1f;
17321872
}
1733-
1734-
lx = winw2, ly = winh2;
1735-
SDL_WarpMouseInWindow(wnd, lx, ly);
17361873
}
17371874
}
17381875

@@ -1741,6 +1878,8 @@ void main_loop()
17411878
mRotate(&view, g.xrot, 0.f, 0.f, 1.f);
17421879
mTranslate(&view, g.pp.x, g.pp.y, g.pp.z);
17431880

1881+
mGetViewZ(&look_dir, view); // refresh
1882+
17441883
//*************************************
17451884
// begin render
17461885
//*************************************
@@ -1861,6 +2000,23 @@ void drawHud()
18612000
// clear cpu hud before rendering to it
18622001
SDL_FillRect(sHud, &sHud->clip_rect, 0x00000000);
18632002

2003+
// window title
2004+
SDL_FillRect(sHud, &(SDL_Rect){0, 0, winw, 19}, 0xDDd0e040);
2005+
SDL_FillRect(sHud, &(SDL_Rect){1, 1, winw-2, 17}, 0xBBa0b010);
2006+
const uint len = lenText("Voxel Paint");
2007+
drawText(sHud, "Voxel Paint", winw2-24, 3, 2);
2008+
drawText(sHud, "Voxel Paint", winw2-26, 3, 2);
2009+
drawText(sHud, "Voxel Paint", winw2-25, 5, 2);
2010+
drawText(sHud, "Voxel Paint", winw2-25, 4, 0);
2011+
drawText(sHud, "X -", 5, 3, 3);
2012+
drawText(sHud, "X -", 4, 4, 0);
2013+
drawText(sHud, "- X", winw-23, 3, 3);
2014+
drawText(sHud, "- X", winw-22, 4, 0);
2015+
SDL_FillRect(sHud, &(SDL_Rect){winw-15, winh-15, 15, 15}, 0xDDd0e040);
2016+
SDL_FillRect(sHud, &(SDL_Rect){winw-14, winh-14, 13, 13}, 0xBBa0b010);
2017+
drawText(sHud, "r", winw-8, winh-12, 3);
2018+
drawText(sHud, "r", winw-9, winh-13, 0);
2019+
18642020
// pixel crosshair
18652021
// setpixel(sHud, winw2, winh2, 0xFFFFFF00);
18662022
// setpixel(sHud, winw2+1, winh2, 0xFFFFFF00);
@@ -1879,11 +2035,12 @@ void drawHud()
18792035
// fps
18802036
char tmp[16];
18812037
sprintf(tmp, "%u", g_fps);
1882-
SDL_FillRect(sHud, &(SDL_Rect){0, 0, lenText(tmp)+8, 19}, 0xCC000000);
1883-
drawText(sHud, tmp, 4, 4, 2);
2038+
SDL_FillRect(sHud, &(SDL_Rect){0, 19, lenText(tmp)+8, 19}, 0xCC000000);
2039+
drawText(sHud, tmp, 4, 23, 2);
18842040
// center hud
18852041
const int top = winh2-(11*12);
18862042
const int left = winw2-239;
2043+
SDL_FillRect(sHud, &(SDL_Rect){winw2-255, top-5, 510, 280}, 0xBBa0b010);
18872044
SDL_FillRect(sHud, &(SDL_Rect){winw2-250, top, 500, 270}, 0xCC000000);
18882045
int a = drawText(sHud, "Voxel Paint", winw2-27, top+11, 3);
18892046
a = drawText(sHud, appVersion, left+455, top+11, 4);
@@ -1978,7 +2135,7 @@ int main(int argc, char** argv)
19782135
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
19792136
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
19802137
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
1981-
wnd = SDL_CreateWindow(appTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, winw, winh, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
2138+
wnd = SDL_CreateWindow(appTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, winw, winh, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS);
19822139
if(wnd == NULL)
19832140
{
19842141
printf("ERROR: SDL_CreateWindow(): %s\n", SDL_GetError());
@@ -2047,7 +2204,7 @@ int main(int argc, char** argv)
20472204
printf("----\n");
20482205
printf("James William Fletcher (github.com/mrbid)\n");
20492206
printf("----\n\n");
2050-
printf("Mouse locks when you click on the game window, press ESCAPE to unlock the mouse.\n\n");
2207+
printf("Mouse locks when you click on the game window, press ESCAPE or TAB to unlock the mouse.\n\n");
20512208
printf("W,A,S,D = Move around based on relative orientation to X and Y.\n");
20522209
printf("SPACE + L-SHIFT = Move up and down relative Z.\n");
20532210
printf("Left Click / R-SHIFT = Place node.\n");

0 commit comments

Comments
 (0)