-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.cpp
545 lines (475 loc) · 15.5 KB
/
mouse.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// TODO Fix memory leak and crashing errors when dragging and dropping clips
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include "resource.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_gfxPrimitives.h>
#include <SDL2/SDL_ttf.h>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/avconfig.h>
#include <libswresample/swresample.h>
}
#include "xtrace.h" // NOTE: See note inside xtrace.h (for Visual Studio debugging)
#include "util.h"
#include "datatypes.h"
#include "colors.h"
#include "testvideos.h"
#define KEYUP_CTRL
global int Global_screenWidth = 1920;
global int Global_screenHeight = 1080;
global SDL_Rect Global_screenRect = {};
global bool Global_running = true;
global bool Global_paused = true;
global bool Global_flush = false;
global int Global_playIndex = 0;
global int Global_seekIndex = 0;
global int Global_clipNumbers = 0;
global SDL_Window *Global_window;
global SDL_Renderer *Global_renderer;
global bool Global_drawClipBoundRect = true;
global SDL_Point Global_origVideoPoint;
global SDL_AudioDeviceID Global_AudioDeviceID = {};
global SDL_AudioSpec Global_AudioSpec = {};
#include "ui.h"
#include "video.h"
#include "audio.h"
global ViewRects Global_views = {};
global VideoFile Global_videoFile = {};
global VideoClip Global_videoClip = {};
struct Mouse
{
int32 x;
int32 y;
SDL_Point click;
bool down;
};
inline Mouse createMouse()
{
Mouse mouse = {0};
mouse.click = {-1, -1};
return mouse;
}
internal void newClip(const char *name)
{
Global_playIndex = 0;
freeVideoClip(&Global_videoClip);
freeVideoFile(&Global_videoFile);
loadVideoFile(&Global_videoFile, Global_renderer, name);
printVideoFileInfo(Global_videoFile);
createVideoClip(&Global_videoClip, &Global_videoFile, Global_renderer, Global_clipNumbers++);
printVideoClipInfo(Global_videoClip);
layoutWindowElements(Global_window, &Global_views, &Global_videoClip, Global_playIndex);
}
void seek_initial(int amount)
{
Global_paused = true;
Global_seekIndex = Global_playIndex;
int index = Global_playIndex + amount;
if(index > Global_videoClip.endFrame) index = Global_videoClip.endFrame;
else if(index < 0) index = 0;
else if(index == Global_videoClip.endFrame) return;
else if(index == 0) return;
setScrubberXPosition(Global_videoClip, &Global_views, Global_playIndex);
Global_playIndex = index;
}
// NOTE: As of this point, refreshing all window elements is actually fast enough when updating the
// clips. This is not really a problem right now as the code to do this is really only for testing.
// Remember, however, any time a new clip is loaded the rectangles for the composite view and
// current views must be updated so it can resize the clip's aspect ratio correctly.
internal void HandleEvents(Mouse *mouse, SDL_Event event, VideoClip *clip, char **fname)
{
SDL_PumpEvents();
SDL_GetMouseState(&mouse->x, &mouse->y);
if(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT) Global_running = false;
if(event.type == SDL_MOUSEMOTION)
{
SDL_GetMouseState(&mouse->x, &mouse->y);
if(mouse->down)
{
if(SDL_PointInRect(&mouse->click, &Global_videoClip.tlRect))
{
float percent = (float)(mouse->x - clip->tlRect.x) / clip->tlRect.w;
int wantedFrame = (float)clip->endFrame * percent;
if(wantedFrame < 0) wantedFrame = 0;
if(wantedFrame > clip->endFrame) wantedFrame = clip->endFrame;
if(seekToAnyFrame(&Global_videoClip, wantedFrame))
{
Global_playIndex = wantedFrame;
setScrubberXPosition(Global_videoClip, &Global_views, Global_playIndex);
}
}
}
}
if(event.type == SDL_MOUSEBUTTONDOWN)
{
mouse->down = true;
mouse->click.x = mouse->x;
mouse->click.y = mouse->y;
Global_origVideoPoint.x = Global_videoClip.videoRect.x;
Global_origVideoPoint.y = Global_videoClip.videoRect.y;
}
if(event.type == SDL_MOUSEBUTTONUP)
{
mouse->down = false;
if(SDL_PointInRect(&mouse->click, &Global_videoClip.tlRect))
{
float percent = (float)(mouse->x - clip->tlRect.x) / clip->tlRect.w;
int wantedFrame = (float)clip->endFrame * percent;
if(wantedFrame < 0) wantedFrame = 0;
if(wantedFrame > clip->endFrame) wantedFrame = clip->endFrame;
if(seekToAnyFrame(&Global_videoClip, wantedFrame))
{
Global_playIndex = wantedFrame;
setScrubberXPosition(Global_videoClip, &Global_views, Global_playIndex);
}
}
mouse->click.x = -1;
mouse->click.y = -1;
}
if(event.type == SDL_KEYDOWN)
{
SDL_Keycode key = event.key.keysym.sym;
switch(key)
{
case SDLK_ESCAPE:
{
Global_running = false;
} break;
case SDLK_SPACE:
{
if(!Global_paused) Global_paused = true;
else Global_paused = false;
} break;
case SDLK_RIGHT:
case SDLK_f:
{
seek_initial(1);
} break;
case SDLK_LEFT:
case SDLK_d:
{
seek_initial(-1);
} break;
case SDLK_r:
case SDLK_UP:
{
seek_initial(10);
} break;
case SDLK_e:
case SDLK_DOWN:
{
seek_initial(-10);
} break;
case SDLK_HOME:
{
seek_initial(-Global_playIndex);
} break;
case SDLK_END:
{
seek_initial(Global_videoClip.endFrame - Global_playIndex);
} break;
case SDLK_v:
{
Global_videoClip.videoRect.w = Global_videoClip.vfile->width;
Global_videoClip.videoRect.h = Global_videoClip.vfile->height;
Global_videoClip.videoRect.x = Global_views.background.x +
((Global_views.background.w - Global_videoClip.videoRect.w) / 2);
Global_videoClip.videoRect.y = Global_views.background.y +
((Global_views.background.h - Global_videoClip.videoRect.h) / 2);
} break;
case SDLK_b:
{
if(!Global_drawClipBoundRect) Global_drawClipBoundRect = true;
else Global_drawClipBoundRect = false;
} break;
}
}
if(event.type == SDL_KEYUP)
{
SDL_Keycode key = event.key.keysym.sym;
switch(key)
{
case SDLK_RIGHT:
case SDLK_f:
case SDLK_LEFT:
case SDLK_d:
case SDLK_r:
case SDLK_UP:
case SDLK_e:
case SDLK_DOWN:
case SDLK_HOME:
case SDLK_END:
{
seekToAnyFrame(&Global_videoClip, Global_playIndex);
} break;
}
}
if(event.type == SDL_MOUSEWHEEL)
{
int amount = 100;
if(event.wheel.y < 0) amount = -amount;
Global_videoClip.videoRect.w += amount;
Global_videoClip.videoRect.h += amount;
}
if(event.type == SDL_WINDOWEVENT)
{
switch(event.window.event)
{
case SDL_WINDOWEVENT_RESIZED:
{
layoutWindowElements(Global_window, &Global_views, &Global_videoClip, Global_playIndex);
} break;
case SDL_WINDOWEVENT_FOCUS_LOST:
{
Global_paused = true;
} break;
}
}
if(event.type == SDL_DROPFILE)
{
// TODO Fix drag and drop up so there are no more crashes.
*fname = event.drop.file;
Global_playIndex = 0;
freeVideoClip(&Global_videoClip);
freeVideoFile(&Global_videoFile);
loadVideoFile(&Global_videoFile, Global_renderer, *fname);
printVideoFileInfo(Global_videoFile);
createVideoClip(&Global_videoClip, &Global_videoFile, Global_renderer, Global_clipNumbers++);
printVideoClipInfo(Global_videoClip);
// MUST Layout Window Elements so the video and scrubber are in the correct place
layoutWindowElements(Global_window, &Global_views, &Global_videoClip, Global_playIndex);
}
}
}
void WaitForDroppedFileEvent(SDL_Event event, char **fname, bool *gotFile)
{
SDL_PumpEvents();
if(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_DROPFILE:
{
*fname = event.drop.file;
*gotFile = true;
} break;
case SDL_KEYDOWN:
{
SDL_Keycode key = event.key.keysym.sym;
if(key == SDLK_ESCAPE) Global_running = false;
} break;
case SDL_QUIT:
{
Global_running = false;
} break;
}
}
}
int main(int argc, char **argv)
{
printf("Hello world.\n\n");
av_register_all();
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
Global_window = SDL_CreateWindow("Mouse",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
Global_screenWidth, Global_screenHeight,
SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE
|SDL_WINDOW_MAXIMIZED);
int windowWidth, windowHeight;
Global_renderer = SDL_CreateRenderer(Global_window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event event = {};
Global_screenRect.x = 0;
Global_screenRect.y = 0;
Global_screenRect.w = Global_screenWidth;
Global_screenRect.h = Global_screenHeight;
char *fname = "";
// If we have an argument then we try to use it as a filename, otherwise we go to the loop.
if(argv[1]) fname = argv[1];
else
{
bool gotFile = false;
// We didn't get a file from argv[1] so open the window and render it and wait for a file
// to be dropped on the window.
while(!gotFile && Global_running)
{
WaitForDroppedFileEvent(event, &fname, &gotFile);
SDL_SetRenderDrawColor(Global_renderer,
tcBackground.r,
tcBackground.g,
tcBackground.b,
tcBackground.a);
SDL_RenderClear(Global_renderer);
SDL_RenderPresent(Global_renderer);
}
// Never got a filename (user gave an exit code without dropping a file).
// Just skip everything and exit.
if(!gotFile) return -1;
}
// Global_AudioDeviceID = initAudioDevice(Global_AudioSpec); WARNING XXX FIXME Breaks SDL_Quit
loadVideoFile(&Global_videoFile, Global_renderer, fname);
printVideoFileInfo(Global_videoFile);
createVideoClip(&Global_videoClip, &Global_videoFile, Global_renderer, Global_clipNumbers++);
printVideoClipInfo(Global_videoClip);
// MUST Layout Window Elements so the video and scrubber are in the correct place
layoutWindowElements(Global_window, &Global_views, &Global_videoClip, Global_playIndex);
TTF_Init();
if(!TTF_WasInit())
{
printf("Could not initalize TTF library!\nExiting.\n");
return -1;
}
TTF_Font *fontDroidSansMono24 = TTF_OpenFont("../res/fonts/DroidSansMono.ttf", 24);
TTF_Font *fontDroidSansMono32 = TTF_OpenFont("../res/fonts/DroidSansMono.ttf", 32);
SDL_Surface *playIndexSurface;
SDL_Surface *filenameSurface;
SDL_Surface *ticksElapsedSurface;
SDL_Surface *mousePosSurface;
Mouse mouse = createMouse();
float ticksElapsed = 0.0f;
int startTicks = SDL_GetTicks();
while(Global_running)
{
HandleEvents(&mouse, event, &Global_videoClip, &fname);
SDL_GetWindowSize(Global_window, &windowWidth, &windowHeight);
SDL_SetRenderDrawColor(Global_renderer,
tcBackground.r,
tcBackground.g,
tcBackground.b,
tcBackground.a);
SDL_RenderClear(Global_renderer);
setRenderColor(Global_renderer, tcBlack);
SDL_RenderFillRect(Global_renderer, &Global_views.background);
//setRenderColor(Global_renderer, tcView);
//SDL_RenderFillRect(Global_renderer, &Global_views.timeline);
#if 1
if(!Global_paused)
{
int endTicks = SDL_GetTicks();
ticksElapsed += (float)endTicks - (float)startTicks;
if(ticksElapsed >=
(Global_videoClip.vfile->msperframe - (Global_videoClip.vfile->msperframe * 0.05f)))
{
int res = decodeSingleFrame(&Global_videoClip);
if(res > 0)
{
updateVideoClipTexture(&Global_videoClip);
Global_playIndex++;
setScrubberXPosition(Global_videoClip, &Global_views, Global_playIndex);
}
else
{
Global_flush = true;
}
ticksElapsed = 0;
}
}
startTicks = SDL_GetTicks();
#endif
int filenameTextHeight;
TTF_SizeText(fontDroidSansMono24, Global_videoClip.filename, NULL, &filenameTextHeight);
int fnameTextX = Global_views.background.x;
int fnameTextY = Global_views.background.y - filenameTextHeight - 8;
DrawText(Global_renderer, fontDroidSansMono24, fnameTextX, fnameTextY,
Global_videoClip.filename, SDLC_white);
int playHeadW;
char playheadTextBuffer[32];
sprintf(playheadTextBuffer, "%d", Global_playIndex);
TTF_SizeText(fontDroidSansMono24, playheadTextBuffer, &playHeadW, NULL);
int playHeadX = Global_views.scrubber.x - (playHeadW / 2);
int playHeadY = Global_views.scrubber.y + Global_views.scrubber.h;
DrawText(Global_renderer, fontDroidSansMono24, playHeadX, playHeadY,
playheadTextBuffer, SDLC_white);
// < DEBUG
#if 0
char ticksElapsedBuffer[32];
if(ticksElapsed < 10.0f) sprintf(ticksElapsedBuffer, "Ticks: %.4f", ticksElapsed);
else sprintf(ticksElapsedBuffer, "Ticks: %.4f", ticksElapsed);
if(ticksElapsedSurface = TTF_RenderText_Blended(fontDroidSansMono24,
ticksElapsedBuffer, SDLC_white))
{
int w;
TTF_SizeText(fontDroidSansMono24, "Ticks: 00.0000", &w, NULL);
SDL_Rect ticksElapsedRect;
ticksElapsedRect.w = ticksElapsedSurface->w;
ticksElapsedRect.h = ticksElapsedSurface->h;
ticksElapsedRect.x = Global_views.background.x +
Global_views.background.w - w;
ticksElapsedRect.y = Global_views.background.y - ticksElapsedRect.h;
SDL_Texture *ticksElapsedTexture = SDL_CreateTextureFromSurface(Global_renderer,
ticksElapsedSurface);
SDL_RenderCopy(Global_renderer, ticksElapsedTexture, NULL, &ticksElapsedRect);
SDL_DestroyTexture(ticksElapsedTexture);
SDL_FreeSurface(ticksElapsedSurface);
}
#endif
// > DEBUG
// < DEBUG
#if 0
char mousePosBuffer[32];
sprintf(mousePosBuffer, "Mouse: (%d,%d)", Global_mousex, Global_mousey);
if(mousePosSurface = TTF_RenderText_Blended(fontDroidSansMono24, mousePosBuffer, SDLC_white))
{
SDL_Rect mousePosRect;
mousePosRect.w = mousePosSurface->w;
mousePosRect.h = mousePosSurface->h;
mousePosRect.x = 8;
mousePosRect.y = 2;
SDL_Texture *mousePosTexture = SDL_CreateTextureFromSurface(Global_renderer, mousePosSurface);
SDL_RenderCopy(Global_renderer, mousePosTexture, NULL, &mousePosRect);
SDL_DestroyTexture(mousePosTexture);
SDL_FreeSurface(mousePosSurface);
}
#endif
// > DEBUG
SDL_RenderSetClipRect(Global_renderer, &Global_views.background);
SDL_RenderCopy(Global_renderer, Global_videoClip.texture, NULL,
(SDL_Rect *)&Global_videoClip.videoRect);
SDL_RenderSetClipRect(Global_renderer, NULL);
setRenderColor(Global_renderer, tcView);
SDL_RenderFillRect(Global_renderer, &Global_videoClip.tlRect);
setRenderColor(Global_renderer, tcRed);
SDL_RenderFillRect(Global_renderer, &Global_views.scrubber);
setRenderColor(Global_renderer, tcGreen);
SDL_Rect mouser = {mouse.x - 5, mouse.y - 5, 10, 10};
SDL_RenderFillRect(Global_renderer, &mouser);
setRenderColor(Global_renderer, tcBlue);
SDL_Rect clickr = {mouse.click.x - 5, mouse.click.y - 5, 10 , 10};
SDL_RenderFillRect(Global_renderer, &clickr);
if(Global_drawClipBoundRect)
{
// drawClipBoundRect(Global_renderer, Global_videoClip, tcRed, 5); // DEBUG
}
if(mouse.down)
{
SDL_Point p = {mouse.x, mouse.y};
if(SDL_PointInRect(&p, &Global_views.background))
{
Global_videoClip.videoRect.x = Global_origVideoPoint.x + (mouse.x - mouse.click.x);
Global_videoClip.videoRect.y = Global_origVideoPoint.y + (mouse.y - mouse.click.y);
}
}
SDL_RenderPresent(Global_renderer);
}
freeVideoClip(&Global_videoClip);
freeVideoFile(&Global_videoFile);
TTF_CloseFont(fontDroidSansMono24);
TTF_CloseFont(fontDroidSansMono32);
TTF_Quit();
//assert(Global_AudioDeviceID != 0);
//SDL_CloseAudioDevice(Global_AudioDeviceID);
SDL_CloseAudio();
SDL_Quit();
printf("\nGoodbye.\n");
return 0;
}