Files @ r26254:4dd185cf8a2d
Branch filter:

Location: cpp/openttd-patchpack/source/src/video/sdl_v.cpp - annotation

Theleruby
Merge with master
  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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9111:983de9c5a848
r5584:545d748cc681
r8123:dde0a9a84019
r9085:bfc6f61fcf0c
r6937:bea726150bf3
r23587:821735a25dec
r17969:8bce9c292da9
r14248:a9050881acd7
r17271:3e6d78458bdb
r18079:b942dd7f3de8
r22959:8c769e762c2f
r24833:e40e0f8b2230
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r7170:38b143754b40
r7170:38b143754b40
r24236:6391ca82e560
r20036:6a076f22f28a
r5584:545d748cc681
r5584:545d748cc681
r18594:5c0c0176015c
r13257:4c5b8120be59
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20036:6a076f22f28a
r20045:bf3c57e917e9
r5584:545d748cc681
r7170:38b143754b40
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20036:6a076f22f28a
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r18594:5c0c0176015c
r18594:5c0c0176015c
r18594:5c0c0176015c
r18594:5c0c0176015c
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r24236:6391ca82e560
r20036:6a076f22f28a
r24236:6391ca82e560
r20036:6a076f22f28a
r24236:6391ca82e560
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r23376:478a66c91a5e
r20036:6a076f22f28a
r20036:6a076f22f28a
r24236:6391ca82e560
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r24236:6391ca82e560
r23376:478a66c91a5e
r20036:6a076f22f28a
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r25724:cf37407a0fa2
r20036:6a076f22f28a
r5584:545d748cc681
r5584:545d748cc681
r24852:00c1bd78d033
r5584:545d748cc681
r25724:cf37407a0fa2
r24852:00c1bd78d033
r25724:cf37407a0fa2
r6960:e903748e34e4
r25724:cf37407a0fa2
r25724:cf37407a0fa2
r25724:cf37407a0fa2
r25724:cf37407a0fa2
r6960:e903748e34e4
r25724:cf37407a0fa2
r25724:cf37407a0fa2
r25724:cf37407a0fa2
r6960:e903748e34e4
r25724:cf37407a0fa2
r25724:cf37407a0fa2
r6960:e903748e34e4
r25724:cf37407a0fa2
r25724:cf37407a0fa2
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r24851:a41e925d9dc7
r5584:545d748cc681
r22959:8c769e762c2f
r22959:8c769e762c2f
r5584:545d748cc681
r10897:3a9b0eb8375c
r10897:3a9b0eb8375c
r10897:3a9b0eb8375c
r24236:6391ca82e560
r10897:3a9b0eb8375c
r24236:6391ca82e560
r24236:6391ca82e560
r20036:6a076f22f28a
r24236:6391ca82e560
r23376:478a66c91a5e
r10897:3a9b0eb8375c
r24236:6391ca82e560
r20036:6a076f22f28a
r24236:6391ca82e560
r20036:6a076f22f28a
r20036:6a076f22f28a
r24236:6391ca82e560
r23376:478a66c91a5e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10896:feadadf0750a
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r23607:36c15679007d
r23607:36c15679007d
r5584:545d748cc681
r23630:58df4dafed3e
r23630:58df4dafed3e
r23607:36c15679007d
r10896:feadadf0750a
r10896:feadadf0750a
r23376:478a66c91a5e
r23630:58df4dafed3e
r10896:feadadf0750a
r10896:feadadf0750a
r5584:545d748cc681
r10896:feadadf0750a
r12252:2f455828d620
r12252:2f455828d620
r23565:dd2703eca0f6
r23630:58df4dafed3e
r23630:58df4dafed3e
r5584:545d748cc681
r23630:58df4dafed3e
r23630:58df4dafed3e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r12252:2f455828d620
r5584:545d748cc681
r10897:3a9b0eb8375c
r23630:58df4dafed3e
r5584:545d748cc681
r10897:3a9b0eb8375c
r23630:58df4dafed3e
r5584:545d748cc681
r10897:3a9b0eb8375c
r23630:58df4dafed3e
r12252:2f455828d620
r23630:58df4dafed3e
r12252:2f455828d620
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9533:0cf5e972f19e
r9533:0cf5e972f19e
r5584:545d748cc681
r5584:545d748cc681
r20035:c4fe85f9251a
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r21124:6022cb131374
r20036:6a076f22f28a
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r25655:1030dcb7eb52
r6878:61440e99046f
r9470:75edf93b1c69
r5584:545d748cc681
r24524:a57f35a240ed
r24524:a57f35a240ed
r18079:b942dd7f3de8
r24524:a57f35a240ed
r23607:36c15679007d
r18079:b942dd7f3de8
r23376:478a66c91a5e
r5584:545d748cc681
r23376:478a66c91a5e
r23607:36c15679007d
r23376:478a66c91a5e
r18079:b942dd7f3de8
r5584:545d748cc681
r5584:545d748cc681
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r21421:7897be36f196
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r25655:1030dcb7eb52
r20036:6a076f22f28a
r20036:6a076f22f28a
r24236:6391ca82e560
r20036:6a076f22f28a
r23607:36c15679007d
r20045:bf3c57e917e9
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20044:76471a066ad3
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r25655:1030dcb7eb52
r23376:478a66c91a5e
r23376:478a66c91a5e
r20036:6a076f22f28a
r20308:7cc954e96245
r20036:6a076f22f28a
r20036:6a076f22f28a
r20045:bf3c57e917e9
r20045:bf3c57e917e9
r20045:bf3c57e917e9
r20045:bf3c57e917e9
r20045:bf3c57e917e9
r20036:6a076f22f28a
r10897:3a9b0eb8375c
r23376:478a66c91a5e
r23607:36c15679007d
r25655:1030dcb7eb52
r5584:545d748cc681
r10390:00d287c91d28
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r25655:1030dcb7eb52
r23376:478a66c91a5e
r23607:36c15679007d
r25655:1030dcb7eb52
r20036:6a076f22f28a
r20036:6a076f22f28a
r20036:6a076f22f28a
r5584:545d748cc681
r13257:4c5b8120be59
r13257:4c5b8120be59
r13257:4c5b8120be59
r5584:545d748cc681
r5584:545d748cc681
r6878:61440e99046f
r13993:05fba9b1b249
r24236:6391ca82e560
r14151:b74c66bf771d
r20502:41fec0dd1f60
r20502:41fec0dd1f60
r20502:41fec0dd1f60
r20502:41fec0dd1f60
r20502:41fec0dd1f60
r21124:6022cb131374
r19107:4b7ff0d11293
r14151:b74c66bf771d
r19109:91bf304afe2c
r5584:545d748cc681
r21387:4ac1de161213
r23376:478a66c91a5e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r18395:22ade7a0b9e1
r18395:22ade7a0b9e1
r23376:478a66c91a5e
r18395:22ade7a0b9e1
r18395:22ade7a0b9e1
r18395:22ade7a0b9e1
r24807:48a7f8b46730
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6248:b940b09d7ab8
r5584:545d748cc681
r5584:545d748cc681
r19024:fa664792b0f1
r5584:545d748cc681
r24807:48a7f8b46730
r7311:0facfb3f97ca
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r7311:0facfb3f97ca
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r7311:0facfb3f97ca
r5584:545d748cc681
r5584:545d748cc681
r7311:0facfb3f97ca
r10084:f9afd48f8b5b
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r7310:5e788d82877a
r7310:5e788d82877a
r7311:0facfb3f97ca
r7310:5e788d82877a
r7310:5e788d82877a
r7310:5e788d82877a
r7310:5e788d82877a
r7310:5e788d82877a
r7310:5e788d82877a
r7310:5e788d82877a
r7310:5e788d82877a
r7310:5e788d82877a
r7310:5e788d82877a
r7310:5e788d82877a
r5584:545d748cc681
r5584:545d748cc681
r20633:872eb899e63d
r5584:545d748cc681
r24807:48a7f8b46730
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10897:3a9b0eb8375c
r23088:6710c4c79ac6
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10897:3a9b0eb8375c
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10897:3a9b0eb8375c
r20633:872eb899e63d
r20633:872eb899e63d
r5584:545d748cc681
r5584:545d748cc681
r24939:f00f6879a003
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r24939:f00f6879a003
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r22011:0dc7eed331dd
r23376:478a66c91a5e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r23376:478a66c91a5e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10897:3a9b0eb8375c
r10897:3a9b0eb8375c
r10897:3a9b0eb8375c
r5584:545d748cc681
r10897:3a9b0eb8375c
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20633:872eb899e63d
r20633:872eb899e63d
r20633:872eb899e63d
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r24597:afde5721a3b6
r24597:afde5721a3b6
r13995:533559bea3ff
r5584:545d748cc681
r5584:545d748cc681
r19042:abe79fea164a
r19042:abe79fea164a
r19042:abe79fea164a
r19042:abe79fea164a
r19042:abe79fea164a
r19042:abe79fea164a
r19042:abe79fea164a
r5584:545d748cc681
r24939:f00f6879a003
r24939:f00f6879a003
r5584:545d748cc681
r5584:545d748cc681
r25014:e1f1bf3a062e
r5584:545d748cc681
r5584:545d748cc681
r25014:e1f1bf3a062e
r5584:545d748cc681
r23376:478a66c91a5e
r23376:478a66c91a5e
r23376:478a66c91a5e
r23376:478a66c91a5e
r23376:478a66c91a5e
r23376:478a66c91a5e
r23376:478a66c91a5e
r23376:478a66c91a5e
r23376:478a66c91a5e
r24236:6391ca82e560
r5584:545d748cc681
r24665:20a8caea4459
r24665:20a8caea4459
r12274:46b552dfdb71
r12274:46b552dfdb71
r23376:478a66c91a5e
r12274:46b552dfdb71
r12274:46b552dfdb71
r23376:478a66c91a5e
r25655:1030dcb7eb52
r5584:545d748cc681
r5584:545d748cc681
r20308:7cc954e96245
r13257:4c5b8120be59
r25014:e1f1bf3a062e
r25014:e1f1bf3a062e
r23607:36c15679007d
r5584:545d748cc681
r5584:545d748cc681
r20308:7cc954e96245
r20308:7cc954e96245
r23376:478a66c91a5e
r23376:478a66c91a5e
r20308:7cc954e96245
r20308:7cc954e96245
r7170:38b143754b40
r5584:545d748cc681
r23376:478a66c91a5e
r23376:478a66c91a5e
r23376:478a66c91a5e
r23376:478a66c91a5e
r5584:545d748cc681
r5584:545d748cc681
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24970:1ff44d19fa4a
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24970:1ff44d19fa4a
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r24849:01243a72f255
r7170:38b143754b40
r5584:545d748cc681
r25014:e1f1bf3a062e
r25014:e1f1bf3a062e
r5584:545d748cc681
r13431:72f7a43f410b
r5584:545d748cc681
r25011:61d28a13bb41
r24855:196c88971d0d
r5584:545d748cc681
r25014:e1f1bf3a062e
r25014:e1f1bf3a062e
r5584:545d748cc681
r5584:545d748cc681
r7170:38b143754b40
r5584:545d748cc681
r23586:11b470d4cee6
r5584:545d748cc681
r5584:545d748cc681
r8171:ff11fcdf6589
r5584:545d748cc681
r7170:38b143754b40
r5584:545d748cc681
r23630:58df4dafed3e
r21263:87b7f54217b8
r21263:87b7f54217b8
r10897:3a9b0eb8375c
r5584:545d748cc681
r5584:545d748cc681
r21263:87b7f54217b8
r25007:c39f47be967b
r21263:87b7f54217b8
r5584:545d748cc681
r5584:545d748cc681
r18160:133e96e28508
r18160:133e96e28508
r22568:df7ef8916103
r22568:df7ef8916103
r22568:df7ef8916103
r5988:7f6f0097eeb1
/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file sdl_v.cpp Implementation of the SDL video driver. */

#ifdef WITH_SDL

#include "../stdafx.h"
#include "../openttd.h"
#include "../gfx_func.h"
#include "../rev.h"
#include "../blitter/factory.hpp"
#include "../thread.h"
#include "../progress.h"
#include "../core/random_func.hpp"
#include "../core/math_func.hpp"
#include "../fileio_func.h"
#include "../framerate_type.h"
#include "../window_func.h"
#include "sdl_v.h"
#include <SDL.h>

#include "../safeguards.h"

static FVideoDriver_SDL iFVideoDriver_SDL;

static SDL_Surface *_sdl_surface;
static SDL_Surface *_sdl_realscreen;
static bool _all_modes;

static Palette _local_palette;

#define MAX_DIRTY_RECTS 100
static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
static int _num_dirty_rects;
static int _use_hwpalette;
static int _requested_hwpalette; /* Did we request a HWPALETTE for the current video mode? */

void VideoDriver_SDL::MakeDirty(int left, int top, int width, int height)
{
	if (_num_dirty_rects < MAX_DIRTY_RECTS) {
		_dirty_rects[_num_dirty_rects].x = left;
		_dirty_rects[_num_dirty_rects].y = top;
		_dirty_rects[_num_dirty_rects].w = width;
		_dirty_rects[_num_dirty_rects].h = height;
	}
	_num_dirty_rects++;
}

static void UpdatePalette(bool init = false)
{
	SDL_Color pal[256];

	for (int i = 0; i != _local_palette.count_dirty; i++) {
		pal[i].r = _local_palette.palette[_local_palette.first_dirty + i].r;
		pal[i].g = _local_palette.palette[_local_palette.first_dirty + i].g;
		pal[i].b = _local_palette.palette[_local_palette.first_dirty + i].b;
		pal[i].unused = 0;
	}

	SDL_SetColors(_sdl_surface, pal, _local_palette.first_dirty, _local_palette.count_dirty);

	if (_sdl_surface != _sdl_realscreen && init) {
		/* When using a shadow surface, also set our palette on the real screen. This lets SDL
		 * allocate as many colors (or approximations) as
		 * possible, instead of using only the default SDL
		 * palette. This allows us to get more colors exactly
		 * right and might allow using better approximations for
		 * other colors.
		 *
		 * Note that colors allocations are tried in-order, so
		 * this favors colors further up into the palette. Also
		 * note that if two colors from the same animation
		 * sequence are approximated using the same color, that
		 * animation will stop working.
		 *
		 * Since changing the system palette causes the colours
		 * to change right away, and allocations might
		 * drastically change, we can't use this for animation,
		 * since that could cause weird coloring between the
		 * palette change and the blitting below, so we only set
		 * the real palette during initialisation.
		 */
		SDL_SetColors(_sdl_realscreen, pal, _local_palette.first_dirty, _local_palette.count_dirty);
	}

	if (_sdl_surface != _sdl_realscreen && !init) {
		/* We're not using real hardware palette, but are letting SDL
		 * approximate the palette during shadow -> screen copy. To
		 * change the palette, we need to recopy the entire screen.
		 *
		 * Note that this operation can slow down the rendering
		 * considerably, especially since changing the shadow
		 * palette will need the next blit to re-detect the
		 * best mapping of shadow palette colors to real palette
		 * colors from scratch.
		 */
		SDL_BlitSurface(_sdl_surface, nullptr, _sdl_realscreen, nullptr);
		SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
	}
}

static void InitPalette()
{
	CopyPalette(_local_palette, true);
	UpdatePalette(true);
}

void VideoDriver_SDL::CheckPaletteAnim()
{
	if (!CopyPalette(_local_palette)) return;

	Blitter *blitter = BlitterFactory::GetCurrentBlitter();

	switch (blitter->UsePaletteAnimation()) {
		case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
			UpdatePalette();
			break;

		case Blitter::PALETTE_ANIMATION_BLITTER:
			blitter->PaletteAnimate(_local_palette);
			break;

		case Blitter::PALETTE_ANIMATION_NONE:
			break;

		default:
			NOT_REACHED();
	}
}

void VideoDriver_SDL::Paint()
{
	PerformanceMeasurer framerate(PFE_VIDEO);

	int n = _num_dirty_rects;
	if (n == 0) return;

	_num_dirty_rects = 0;

	if (n > MAX_DIRTY_RECTS) {
		if (_sdl_surface != _sdl_realscreen) {
			SDL_BlitSurface(_sdl_surface, nullptr, _sdl_realscreen, nullptr);
		}

		SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
	} else {
		if (_sdl_surface != _sdl_realscreen) {
			for (int i = 0; i < n; i++) {
				SDL_BlitSurface(_sdl_surface, &_dirty_rects[i], _sdl_realscreen, &_dirty_rects[i]);
			}
		}

		SDL_UpdateRects(_sdl_realscreen, n, _dirty_rects);
	}
}

static const Dimension _default_resolutions[] = {
	{ 640,  480},
	{ 800,  600},
	{1024,  768},
	{1152,  864},
	{1280,  800},
	{1280,  960},
	{1280, 1024},
	{1400, 1050},
	{1600, 1200},
	{1680, 1050},
	{1920, 1200}
};

static void GetVideoModes()
{
	SDL_Rect **modes = SDL_ListModes(nullptr, SDL_SWSURFACE | SDL_FULLSCREEN);
	if (modes == nullptr) usererror("sdl: no modes available");

	_resolutions.clear();

	_all_modes = (SDL_ListModes(nullptr, SDL_SWSURFACE | (_fullscreen ? SDL_FULLSCREEN : 0)) == (void*)-1);
	if (modes == (void*)-1) {
		for (uint i = 0; i < lengthof(_default_resolutions); i++) {
			if (SDL_VideoModeOK(_default_resolutions[i].width, _default_resolutions[i].height, 8, SDL_FULLSCREEN) != 0) {
				_resolutions.push_back(_default_resolutions[i]);
			}
		}
	} else {
		for (int i = 0; modes[i]; i++) {
			uint w = modes[i]->w;
			uint h = modes[i]->h;
			if (w < 640 || h < 480) continue; // reject too small resolutions
			if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(w, h)) != _resolutions.end()) continue;
			_resolutions.emplace_back(w, h);
		}
		if (_resolutions.empty()) usererror("No usable screen resolutions found!\n");
		SortResolutions();
	}
}

static void GetAvailableVideoMode(uint *w, uint *h)
{
	/* All modes available? */
	if (_all_modes || _resolutions.empty()) return;

	/* Is the wanted mode among the available modes? */
	if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;

	/* Use the closest possible resolution */
	uint best = 0;
	uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
	for (uint i = 1; i != _resolutions.size(); ++i) {
		uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
		if (newdelta < delta) {
			best = i;
			delta = newdelta;
		}
	}
	*w = _resolutions[best].width;
	*h = _resolutions[best].height;
}

bool VideoDriver_SDL::CreateMainSurface(uint w, uint h)
{
	SDL_Surface *newscreen, *icon;
	char caption[50];
	int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
	bool want_hwpalette;

	GetAvailableVideoMode(&w, &h);

	Debug(driver, 1, "SDL: using mode {}x{}x{}", w, h, bpp);

	if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");

	std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp");
	if (!icon_path.empty()) {
		/* Give the application an icon */
		icon = SDL_LoadBMP(icon_path.c_str());
		if (icon != nullptr) {
			/* Get the colourkey, which will be magenta */
			uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);

			SDL_SetColorKey(icon, SDL_SRCCOLORKEY, rgbmap);
			SDL_WM_SetIcon(icon, nullptr);
			SDL_FreeSurface(icon);
		}
	}

	if (_use_hwpalette == 2) {
		/* Default is to autodetect when to use SDL_HWPALETTE.
		 * In this case, SDL_HWPALETTE is only used for 8bpp
		 * blitters in fullscreen.
		 *
		 * When using an 8bpp blitter on a 8bpp system in
		 * windowed mode with SDL_HWPALETTE, OpenTTD will claim
		 * the system palette, making all other applications
		 * get the wrong colours. In this case, we're better of
		 * trying to approximate the colors we need using system
		 * colors, using a shadow surface (see below).
		 *
		 * On a 32bpp system, SDL_HWPALETTE is ignored, so it
		 * doesn't matter what we do.
		 *
		 * When using a 32bpp blitter on a 8bpp system, setting
		 * SDL_HWPALETTE messes up rendering (at least on X11),
		 * so we don't do that. In this case, SDL takes care of
		 * color approximation using its own shadow surface
		 * (which we can't force in 8bpp on 8bpp mode,
		 * unfortunately).
		 */
		want_hwpalette = bpp == 8 && _fullscreen && _support8bpp == S8BPP_HARDWARE;
	} else {
		/* User specified a value manually */
		want_hwpalette = _use_hwpalette;
	}

	if (want_hwpalette) Debug(driver, 1, "SDL: requesting hardware palette");

	/* Free any previously allocated shadow surface */
	if (_sdl_surface != nullptr && _sdl_surface != _sdl_realscreen) SDL_FreeSurface(_sdl_surface);

	if (_sdl_realscreen != nullptr) {
		if (_requested_hwpalette != want_hwpalette) {
			/* SDL (at least the X11 driver), reuses the
			 * same window and palette settings when the bpp
			 * (and a few flags) are the same. Since we need
			 * to hwpalette value to change (in particular
			 * when switching between fullscreen and
			 * windowed), we restart the entire video
			 * subsystem to force creating a new window.
			 */
			Debug(driver, 0, "SDL: Restarting SDL video subsystem, to force hwpalette change");
			SDL_QuitSubSystem(SDL_INIT_VIDEO);
			SDL_InitSubSystem(SDL_INIT_VIDEO);
			ClaimMousePointer();
			SetupKeyboard();
		}
	}
	/* Remember if we wanted a hwpalette. We can't reliably query
	 * SDL for the SDL_HWPALETTE flag, since it might get set even
	 * though we didn't ask for it (when SDL creates a shadow
	 * surface, for example). */
	_requested_hwpalette = want_hwpalette;

	/* DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK */
	newscreen = SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | (want_hwpalette ? SDL_HWPALETTE : 0) | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
	if (newscreen == nullptr) {
		Debug(driver, 0, "SDL: Couldn't allocate a window to draw on");
		return false;
	}
	_sdl_realscreen = newscreen;

	if (bpp == 8 && (_sdl_realscreen->flags & SDL_HWPALETTE) != SDL_HWPALETTE) {
		/* Using an 8bpp blitter, if we didn't get a hardware
		 * palette (most likely because we didn't request one,
		 * see above), we'll have to set up a shadow surface to
		 * render on.
		 *
		 * Our palette will be applied to this shadow surface,
		 * while the real screen surface will use the shared
		 * system palette (which will partly contain our colors,
		 * but most likely will not have enough free color cells
		 * for all of our colors). SDL can use these two
		 * palettes at blit time to approximate colors used in
		 * the shadow surface using system colors automatically.
		 *
		 * Note that when using an 8bpp blitter on a 32bpp
		 * system, SDL will create an internal shadow surface.
		 * This shadow surface will have SDL_HWPALLETE set, so
		 * we won't create a second shadow surface in this case.
		 */
		Debug(driver, 1, "SDL: using shadow surface");
		newscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, bpp, 0, 0, 0, 0);
		if (newscreen == nullptr) {
			Debug(driver, 0, "SDL: Couldn't allocate a shadow surface to draw on");
			return false;
		}
	}

	/* Delay drawing for this cycle; the next cycle will redraw the whole screen */
	_num_dirty_rects = 0;

	_screen.width = newscreen->w;
	_screen.height = newscreen->h;
	_screen.pitch = newscreen->pitch / (bpp / 8);
	_screen.dst_ptr = newscreen->pixels;
	_sdl_surface = newscreen;

	/* When in full screen, we will always have the mouse cursor
	 * within the window, even though SDL does not give us the
	 * appropriate event to know this. */
	if (_fullscreen) _cursor.in_window = true;

	Blitter *blitter = BlitterFactory::GetCurrentBlitter();
	blitter->PostResize();

	InitPalette();

	seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision);
	SDL_WM_SetCaption(caption, caption);

	GameSizeChanged();

	return true;
}

bool VideoDriver_SDL::ClaimMousePointer()
{
	SDL_ShowCursor(0);
	return true;
}

struct SDLVkMapping {
	uint16 vk_from;
	byte vk_count;
	byte map_to;
};

#define AS(x, z) {x, 0, z}
#define AM(x, y, z, w) {x, (byte)(y - x), z}

static const SDLVkMapping _vk_mapping[] = {
	/* Pageup stuff + up/down */
	AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
	AS(SDLK_UP,     WKC_UP),
	AS(SDLK_DOWN,   WKC_DOWN),
	AS(SDLK_LEFT,   WKC_LEFT),
	AS(SDLK_RIGHT,  WKC_RIGHT),

	AS(SDLK_HOME,   WKC_HOME),
	AS(SDLK_END,    WKC_END),

	AS(SDLK_INSERT, WKC_INSERT),
	AS(SDLK_DELETE, WKC_DELETE),

	/* Map letters & digits */
	AM(SDLK_a, SDLK_z, 'A', 'Z'),
	AM(SDLK_0, SDLK_9, '0', '9'),

	AS(SDLK_ESCAPE,    WKC_ESC),
	AS(SDLK_PAUSE,     WKC_PAUSE),
	AS(SDLK_BACKSPACE, WKC_BACKSPACE),

	AS(SDLK_SPACE,     WKC_SPACE),
	AS(SDLK_RETURN,    WKC_RETURN),
	AS(SDLK_TAB,       WKC_TAB),

	/* Function keys */
	AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),

	/* Numeric part. */
	AM(SDLK_KP0, SDLK_KP9, '0', '9'),
	AS(SDLK_KP_DIVIDE,   WKC_NUM_DIV),
	AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
	AS(SDLK_KP_MINUS,    WKC_NUM_MINUS),
	AS(SDLK_KP_PLUS,     WKC_NUM_PLUS),
	AS(SDLK_KP_ENTER,    WKC_NUM_ENTER),
	AS(SDLK_KP_PERIOD,   WKC_NUM_DECIMAL),

	/* Other non-letter keys */
	AS(SDLK_SLASH,        WKC_SLASH),
	AS(SDLK_SEMICOLON,    WKC_SEMICOLON),
	AS(SDLK_EQUALS,       WKC_EQUALS),
	AS(SDLK_LEFTBRACKET,  WKC_L_BRACKET),
	AS(SDLK_BACKSLASH,    WKC_BACKSLASH),
	AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),

	AS(SDLK_QUOTE,   WKC_SINGLEQUOTE),
	AS(SDLK_COMMA,   WKC_COMMA),
	AS(SDLK_MINUS,   WKC_MINUS),
	AS(SDLK_PERIOD,  WKC_PERIOD)
};

static uint ConvertSdlKeyIntoMy(SDL_keysym *sym, WChar *character)
{
	const SDLVkMapping *map;
	uint key = 0;

	for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
		if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
			key = sym->sym - map->vk_from + map->map_to;
			break;
		}
	}

	/* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
#if defined(_WIN32) || defined(__OS2__)
	if (sym->scancode == 41) key = WKC_BACKQUOTE;
#elif defined(__APPLE__)
	if (sym->scancode == 10) key = WKC_BACKQUOTE;
#elif defined(__SVR4) && defined(__sun)
	if (sym->scancode == 60) key = WKC_BACKQUOTE;
	if (sym->scancode == 49) key = WKC_BACKSPACE;
#elif defined(__sgi__)
	if (sym->scancode == 22) key = WKC_BACKQUOTE;
#else
	if (sym->scancode == 49) key = WKC_BACKQUOTE;
#endif

	/* META are the command keys on mac */
	if (sym->mod & KMOD_META)  key |= WKC_META;
	if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
	if (sym->mod & KMOD_CTRL)  key |= WKC_CTRL;
	if (sym->mod & KMOD_ALT)   key |= WKC_ALT;

	*character = sym->unicode;
	return key;
}

bool VideoDriver_SDL::PollEvent()
{
	SDL_Event ev;

	if (!SDL_PollEvent(&ev)) return false;

	switch (ev.type) {
		case SDL_MOUSEMOTION:
			if (_cursor.UpdateCursorPosition(ev.motion.x, ev.motion.y, true)) {
				SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
			}
			HandleMouseEvents();
			break;

		case SDL_MOUSEBUTTONDOWN:
			if (_rightclick_emulate && SDL_GetModState() & KMOD_CTRL) {
				ev.button.button = SDL_BUTTON_RIGHT;
			}

			switch (ev.button.button) {
				case SDL_BUTTON_LEFT:
					_left_button_down = true;
					break;

				case SDL_BUTTON_RIGHT:
					_right_button_down = true;
					_right_button_clicked = true;
					break;

				case SDL_BUTTON_WHEELUP:   _cursor.wheel--; break;
				case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; break;

				default: break;
			}
			HandleMouseEvents();
			break;

		case SDL_MOUSEBUTTONUP:
			if (_rightclick_emulate) {
				_right_button_down = false;
				_left_button_down = false;
				_left_button_clicked = false;
			} else if (ev.button.button == SDL_BUTTON_LEFT) {
				_left_button_down = false;
				_left_button_clicked = false;
			} else if (ev.button.button == SDL_BUTTON_RIGHT) {
				_right_button_down = false;
			}
			HandleMouseEvents();
			break;

		case SDL_ACTIVEEVENT:
			if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break;

			if (ev.active.gain) { // mouse entered the window, enable cursor
				_cursor.in_window = true;
			} else {
				UndrawMouseCursor(); // mouse left the window, undraw cursor
				_cursor.in_window = false;
			}
			break;

		case SDL_QUIT:
			HandleExitGameRequest();
			break;

		case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
			if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
					(ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
				ToggleFullScreen(!_fullscreen);
			} else {
				WChar character;
				uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
				HandleKeypress(keycode, character);
			}
			break;

		case SDL_VIDEORESIZE: {
			int w = std::max(ev.resize.w, 64);
			int h = std::max(ev.resize.h, 64);
			CreateMainSurface(w, h);
			break;
		}
		case SDL_VIDEOEXPOSE: {
			/* Force a redraw of the entire screen. Note
			 * that SDL 1.2 seems to do this automatically
			 * in most cases, but 1.3 / 2.0 does not. */
		        _num_dirty_rects = MAX_DIRTY_RECTS + 1;
			break;
		}
	}

	return true;
}

const char *VideoDriver_SDL::Start(const StringList &param)
{
	char buf[30];
	_use_hwpalette = GetDriverParamInt(param, "hw_palette", 2);

	/* Just on the offchance the audio subsystem started before the video system,
	 * check whether any part of SDL has been initialised before getting here.
	 * Slightly duplicated with sound/sdl_s.cpp */
	int ret_code = 0;
	if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
		ret_code = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
	} else if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
		ret_code = SDL_InitSubSystem(SDL_INIT_VIDEO);
	}
	if (ret_code < 0) return SDL_GetError();

	this->UpdateAutoResolution();

	GetVideoModes();
	if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
		return SDL_GetError();
	}

	SDL_VideoDriverName(buf, sizeof buf);
	Debug(driver, 1, "SDL: using driver '{}'", buf);

	MarkWholeScreenDirty();
	SetupKeyboard();

	this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");

	return nullptr;
}

void VideoDriver_SDL::SetupKeyboard()
{
	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
	SDL_EnableUNICODE(1);
}

void VideoDriver_SDL::Stop()
{
	SDL_QuitSubSystem(SDL_INIT_VIDEO);
	if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
		SDL_Quit(); // If there's nothing left, quit SDL
	}
}

void VideoDriver_SDL::InputLoop()
{
	uint32 mod = SDL_GetModState();
	int numkeys;
	Uint8 *keys = SDL_GetKeyState(&numkeys);

	bool old_ctrl_pressed = _ctrl_pressed;

	_ctrl_pressed  = !!(mod & KMOD_CTRL);
	_shift_pressed = !!(mod & KMOD_SHIFT);

#if defined(_DEBUG)
	this->fast_forward_key_pressed = _shift_pressed;
#else
	/* Speedup when pressing tab, except when using ALT+TAB
	 * to switch to another application. */
	this->fast_forward_key_pressed = keys[SDLK_TAB] && (mod & KMOD_ALT) == 0;
#endif /* defined(_DEBUG) */

	/* Determine which directional keys are down. */
	_dirkeys =
		(keys[SDLK_LEFT]  ? 1 : 0) |
		(keys[SDLK_UP]    ? 2 : 0) |
		(keys[SDLK_RIGHT] ? 4 : 0) |
		(keys[SDLK_DOWN]  ? 8 : 0);

	if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
}

void VideoDriver_SDL::MainLoop()
{
	this->StartGameThread();

	for (;;) {
		if (_exit_game) break;

		this->Tick();
		this->SleepTillNextTick();
	}

	this->StopGameThread();
}

bool VideoDriver_SDL::ChangeResolution(int w, int h)
{
	return CreateMainSurface(w, h);
}

bool VideoDriver_SDL::ToggleFullscreen(bool fullscreen)
{
	_fullscreen = fullscreen;
	GetVideoModes(); // get the list of available video modes
	bool ret = !_resolutions.empty() && CreateMainSurface(_cur_resolution.width, _cur_resolution.height);

	if (!ret) {
		/* switching resolution failed, put back full_screen to original status */
		_fullscreen ^= true;
	}

	InvalidateWindowClassesData(WC_GAME_OPTIONS, 3);
	return ret;
}

bool VideoDriver_SDL::AfterBlitterChange()
{
	return CreateMainSurface(_screen.width, _screen.height);
}

#endif /* WITH_SDL */