~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/sdl_canvas_palette_2.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <SDL/SDL.h>
 
3
#include <emscripten.h>
 
4
#include <string.h>
 
5
 
 
6
static const int COLOR_COUNT = 32;
 
7
 
 
8
static SDL_Surface *screen;
 
9
static SDL_Color   pal[COLOR_COUNT +1];
 
10
 
 
11
void pallete(int red, int green, int blue) {
 
12
  //initialize sdl palette
 
13
  //with gradient colors
 
14
  pal[0].r = 0;
 
15
  pal[0].g = 0;
 
16
  pal[0].b = 0;
 
17
  pal[0].unused = 0;
 
18
 
 
19
  for (int i=1; i< 1 + COLOR_COUNT; i++) {
 
20
    pal[i].r = (float) red    / COLOR_COUNT * i;
 
21
    pal[i].g = (float) green  / COLOR_COUNT * i;
 
22
    pal[i].b = (float) blue   / COLOR_COUNT * i;
 
23
    pal[i].unused = 0;
 
24
  }
 
25
 
 
26
  SDL_SetColors(screen, pal, 0, 1 + COLOR_COUNT);
 
27
}
 
28
 
 
29
int main(int argc, char** argv) {
 
30
  SDL_Init(SDL_INIT_VIDEO);
 
31
  screen = SDL_SetVideoMode(600, 450, 8, SDL_HWSURFACE | SDL_HWPALETTE);
 
32
 
 
33
  //test empty pallete
 
34
  SDL_LockSurface(screen);
 
35
  SDL_UnlockSurface(screen);
 
36
 
 
37
  //Draw gradient
 
38
  SDL_LockSurface(screen);
 
39
  int size = screen->h * screen->pitch;
 
40
  char *color   = screen->pixels;
 
41
  int divider  = size / COLOR_COUNT;
 
42
  int i = 0;
 
43
  while (i < size) {
 
44
    *color = 1 + (i / divider); //red
 
45
    color++;
 
46
    i++;
 
47
  }
 
48
  SDL_UnlockSurface(screen);
 
49
 
 
50
  //Set pallete
 
51
  if (argc > 1) {
 
52
    printf("%s\n", argv[1]);
 
53
    if (strcmp(argv[1], "-r") == 0) {
 
54
      printf("set [red]\n");
 
55
      pallete(255, 0, 0);
 
56
    }
 
57
    if (strcmp(argv[1], "-g") == 0) {
 
58
      printf("set [green]\n");
 
59
      pallete(0, 255, 0);
 
60
    }
 
61
    if (strcmp(argv[1], "-b") == 0) {
 
62
      printf("set [blue]\n");
 
63
      pallete(0, 0, 255);
 
64
    }
 
65
  }
 
66
 
 
67
  //refreshing
 
68
  SDL_LockSurface(screen);
 
69
  SDL_UnlockSurface(screen);
 
70
 
 
71
  SDL_Quit();
 
72
 
 
73
  return 0;
 
74
}
 
75