@@ -8,15 +8,15 @@ App app;
88
99int lerp (int a , int b , float t ) { return (int )((float )a + (float )t * ((float )b - (float )a )); }
1010
11- void init ()
11+ void init (Config * cfg )
1212{
1313 if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_EVENTS ) != 0 ) SDL_Log ("%s" , SDL_GetError ());
1414
1515#ifndef __WIN32
1616 app .display = XOpenDisplay (NULL );
1717#endif
1818
19- initWindow ();
19+ initWindow (& app , cfg );
2020
2121 app .renderer =
2222 SDL_CreateRenderer (app .window , -1 , SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
@@ -69,7 +69,7 @@ int main(int argc, char *argv[])
6969 Instance instances [cfg .monitors ];
7070 SDL_Texture * tex [cfg .count ];
7171
72- init ();
72+ init (& cfg );
7373
7474 if (!loadTextures (& cfg , tex )) return 1 ;
7575
@@ -86,12 +86,11 @@ int main(int argc, char *argv[])
8686 instances [i ].buffTex =
8787 SDL_CreateTexture (app .renderer , SDL_PIXELFORMAT_ARGB8888 , SDL_TEXTUREACCESS_TARGET ,
8888 instances [i ].dest .w , instances [i ].dest .h );
89+ instances [i ].finalTex =
90+ SDL_CreateTexture (app .renderer , SDL_PIXELFORMAT_ARGB8888 , SDL_TEXTUREACCESS_TARGET ,
91+ instances [i ].finalDest .w , instances [i ].finalDest .h );
8992 }
9093
91- #ifndef __WIN32
92- SDL_SetRelativeMouseMode (SDL_TRUE );
93- #endif
94-
9594 SDL_Event event ;
9695 int quit = 0 ;
9796
@@ -114,34 +113,35 @@ int main(int argc, char *argv[])
114113 GetCursorPos (& mPos );
115114 mx = mPos .x ;
116115 my = mPos .y ;
116+ #else
117+ SDL_GetGlobalMouseState (& mx , & my );
117118#endif
118119
119120 while (SDL_PollEvent (& event ))
120- {
121121 if (event .type == SDL_QUIT ) quit = 1 ;
122- #ifndef __WIN32
123- else if (event .type == SDL_MOUSEMOTION )
124- {
125- mx = event .motion .x ;
126- my = event .motion .y ;
127- }
128- #endif
129- }
130122
131123 currentX = lerp (currentX , mx , dT * cfg .smooth );
132124 currentY = lerp (currentY , my , dT * cfg .smooth );
133125
134126 for (int u = 0 ; u < cfg .monitors ; u ++ )
135127 {
128+ int relativeCurrentX = currentX - instances [u ].finalDest .x ;
129+ int relativeCurrentY = currentY - instances [u ].finalDest .y ;
130+
131+ if (relativeCurrentX < 0 ) relativeCurrentX = 0 ;
132+ if (relativeCurrentY < 0 ) relativeCurrentY = 0 ;
133+ if (relativeCurrentX > instances [u ].finalDest .w ) relativeCurrentX = instances [u ].finalDest .w ;
134+ if (relativeCurrentY > instances [u ].finalDest .y ) relativeCurrentY = instances [u ].finalDest .h ;
135+
136136 SDL_SetRenderTarget (app .renderer , instances [u ].buffTex );
137137 SDL_RenderClear (app .renderer );
138138
139139 for (int i = 0 ; i < cfg .count ; i ++ )
140140 {
141141 SDL_Rect src = {.x = 0 , .y = 0 , .w = app .srcWidth , .h = app .srcHeight };
142142
143- int x = - ((currentX - instances [u ].dest .w / 2 ) * layerMovX [i ]) * i ;
144- int y = - ((currentY - instances [u ].dest .h / 2 ) * layerMovY [i ]) * i ;
143+ int x = - ((relativeCurrentX - instances [u ].dest .w / 2 ) * layerMovX [i ]);
144+ int y = - ((relativeCurrentY - instances [u ].dest .h / 2 ) * layerMovY [i ]);
145145
146146 for (int j = -1 ; j <= 1 ; j ++ )
147147 {
@@ -154,17 +154,36 @@ int main(int argc, char *argv[])
154154 }
155155 }
156156
157- SDL_SetRenderTarget (app .renderer , NULL );
158- SDL_Rect src = {.x = 0 , .y = 0 , .w = instances [u ].dest .w , .h = instances [u ].dest .h };
157+ SDL_SetRenderTarget (app .renderer , instances [u ].finalTex );
158+ SDL_Rect src = {
159+ .x = 0 ,
160+ .y = 0 ,
161+ .w = instances [u ].dest .w ,
162+ .h = instances [u ].dest .h ,
163+ };
159164
160165 SDL_RenderCopy (app .renderer , instances [u ].buffTex , & src , & instances [u ].dest );
166+
167+ SDL_SetRenderTarget (app .renderer , NULL );
168+ SDL_Rect finalSrc = {
169+ .x = 0 ,
170+ .y = 0 ,
171+ .w = instances [u ].finalDest .w ,
172+ .h = instances [u ].finalDest .h ,
173+ };
174+
175+ SDL_RenderCopy (app .renderer , instances [u ].finalTex , & finalSrc , & instances [u ].finalDest );
161176 }
162177 SDL_RenderPresent (app .renderer );
163178 SDL_Delay (1000 / 60 );
164179 }
165180
166181 for (int i = 0 ; i < cfg .count ; i ++ ) SDL_DestroyTexture (tex [i ]);
167- for (int i = 0 ; i < cfg .monitors ; i ++ ) SDL_DestroyTexture (instances [i ].buffTex );
182+ for (int i = 0 ; i < cfg .monitors ; i ++ )
183+ {
184+ SDL_DestroyTexture (instances [i ].buffTex );
185+ SDL_DestroyTexture (instances [i ].finalTex );
186+ }
168187#ifndef __WIN32
169188 XCloseDisplay (app .display );
170189#endif
0 commit comments