Method: Rug.start

Defined in:
ext/rug.c

.startObject



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
# File 'ext/rug.c', line 45

static VALUE RugStart(VALUE class){
  SDL_Init(SDL_INIT_VIDEO);
  atexit(SDL_Quit);

  SDL_Event ev;
  int lastDraw = 0;

  int frameGap = RugConf.frameGap;

  mainWnd = DoConf();

  if (loadFunc != Qnil){
    rb_funcall(loadFunc, rb_intern("call"), 0);
  }

  // TODO: make background configurable
  Uint32 black = SDL_MapRGB(mainWnd->format, 0, 0, 0);

  while (1){
    if (SDL_PollEvent(&ev)){
      if (!HandleEvent(ev)){
        break;
      }
    }

    int now = SDL_GetTicks();
    if (lastDraw + frameGap < now){
      // update
      if (updateFunc != Qnil){
        rb_funcall(updateFunc, rb_intern("call"), 1, INT2NUM(now - lastDraw));
      }

      // render
      SDL_FillRect(mainWnd, NULL, black);

      if (drawFunc != Qnil){
        rb_funcall(drawFunc, rb_intern("call"), 0);
      }
      SDL_UpdateRect(mainWnd, 0, 0, 0, 0);
      lastDraw = now;
    }

    SDL_Delay(1);
  }

  return Qnil;
}