File: test_sdl_set_clip_rect.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (97 lines) | stat: -rw-r--r-- 3,053 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright 2014 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

#include <stdio.h>
#include <SDL/SDL.h>
#include <assert.h>
#include <stdint.h>
#define width 320
#define height 240

int main() {
  SDL_Rect rect = {0, 0, width, height};
  SDL_Rect firstRect = {10, 10, 50, 100};
  SDL_Rect secondRect = {30, 50, 100, 100};

  SDL_Rect firstRectForFill = {240 + 10, 10, 50, 100};
  SDL_Rect secondRectForFill = {240 + 30, 50, 100, 100};

  SDL_Rect rectForTest = {0, 0, 0, 0};
  SDL_Rect lastRect = { 100, 150, 120, 40};

  SDL_Init(SDL_INIT_VIDEO);
  SDL_Surface *dst = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE);
  SDL_Surface *src = SDL_CreateRGBSurface(0, width, height, 32, 
    0x000000ff, 
    0x0000ff00, 
    0x00ff0000, 
    0xff000000);

  /* Fill dst with yellow color */
  SDL_FillRect(src, &rect, SDL_MapRGB(src->format, 255, 255, 0));
  SDL_FillRect(dst, &rect, SDL_MapRGB(dst->format, 0, 0, 0));
  SDL_SetClipRect(dst, NULL);
  SDL_BlitSurface(src, &rect, dst, &rect);

  /* Draw red on dst */
  SDL_FillRect(src, &rect, SDL_MapRGB(src->format, 255, 0, 0));
  SDL_SetClipRect(dst, &firstRect);
  SDL_BlitSurface(src, &rect, dst, &rect);

  assert(rect.x = firstRect.x);
  assert(rect.y = firstRect.y);
  assert(rect.w = firstRect.w);
  assert(rect.h = firstRect.h);

  /* Draw green rect on red rect */
  SDL_FillRect(src, &rect, SDL_MapRGB(src->format, 0, 255, 0));
  SDL_SetClipRect(dst, &secondRect);
  SDL_BlitSurface(src, &rect, dst, &rect);

  assert(rect.x = secondRect.x);
  assert(rect.y = secondRect.y);
  assert(rect.w = firstRect.x + firstRect.w);
  assert(rect.h = firstRect.h + firstRect.h);

  /* Same with fill rect */
  rect.x = 0; rect.y = 0;
  rect.w = width; rect.h = height;

  SDL_SetClipRect(dst, &firstRectForFill);
  SDL_FillRect(dst, &rect, SDL_MapRGB(dst->format, 0, 0, 255));

  assert(rect.x = firstRectForFill.x);
  assert(rect.y = firstRectForFill.y);
  assert(rect.w = firstRectForFill.w);
  assert(rect.h = firstRectForFill.h);

  SDL_SetClipRect(dst, &secondRectForFill);
  SDL_FillRect(dst, &rect, SDL_MapRGBA(dst->format, 255, 0, 255, 255));

  assert(rect.x = secondRectForFill.x);
  assert(rect.y = secondRectForFill.y);
  assert(rect.w = firstRectForFill.x + firstRectForFill.w);
  assert(rect.h = firstRectForFill.h + firstRectForFill.h);

  SDL_GetClipRect(dst, &rectForTest);
  assert(rectForTest.x == 270);
  assert(rectForTest.y == 50);
  assert(rectForTest.w == 50);
  assert(rectForTest.h == 100);

  SDL_SetClipRect(dst, 0);
  SDL_FillRect(dst, &lastRect, SDL_MapRGBA(dst->format, 255, 0, 0, 255));
  SDL_UpdateRect(dst, 0, 0, width, height);

  printf("There should be yellow background\n");
  printf("One the left side there should be red rect with green rect inside\n");
  printf("One the right side there should be blue rect with pink rect inside\n");

  SDL_Quit();

  return 0;
}