| 1 | /* |
|---|
| 2 | * Copyright (c) 2004-2006 Kazunori "jagarl" Ueno |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | * modification, are permitted provided that the following conditions |
|---|
| 7 | * are met: |
|---|
| 8 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 9 | * notice, this list of conditions and the following disclaimer. |
|---|
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 12 | * documentation and/or other materials provided with the distribution. |
|---|
| 13 | * 3. The name of the author may not be used to endorse or promote products |
|---|
| 14 | * derived from this software without specific prior written permission. |
|---|
| 15 | * |
|---|
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|---|
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|---|
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|---|
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|---|
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|---|
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | #include <SDL.h> |
|---|
| 29 | #include "system.h" |
|---|
| 30 | #include <iostream> |
|---|
| 31 | #include <stdio.h> |
|---|
| 32 | |
|---|
| 33 | using namespace std; |
|---|
| 34 | |
|---|
| 35 | // void SDL_SetEventFilter(SDL_EventFilter filter); |
|---|
| 36 | // typedef int (*SDL_EventFilter)(const SDL_Event *event); |
|---|
| 37 | namespace System { |
|---|
| 38 | Main* Main::instance = NULL; |
|---|
| 39 | |
|---|
| 40 | int Main::event_filter(const SDL_Event* event) { |
|---|
| 41 | return 1; /* throw all event */ |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | Main::Main(void) { |
|---|
| 45 | instance = this; |
|---|
| 46 | framerate = 20; |
|---|
| 47 | cursor = NULL; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | Main::~Main() { |
|---|
| 51 | if (cursor) delete cursor; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | void Main::Quit(void) { |
|---|
| 55 | is_exit = true; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | void Main::EnableVideo(void) { |
|---|
| 59 | is_video_update = true; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | void Main::DisableVideo(void) { |
|---|
| 63 | is_video_update = false; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | bool Main::is_exit = false; |
|---|
| 67 | bool Main::is_video_update = true; |
|---|
| 68 | |
|---|
| 69 | void Main::Mainloop(void) { |
|---|
| 70 | SDL_SetEventFilter(&event_filter); |
|---|
| 71 | Uint32 old_time = 0; |
|---|
| 72 | while(!is_exit) { |
|---|
| 73 | Uint32 start_time = SDL_GetTicks(); |
|---|
| 74 | if (! event.Exec(start_time)) break; |
|---|
| 75 | if (start_time - old_time > 1000/framerate) { |
|---|
| 76 | if (is_video_update) root.ExecUpdate(); |
|---|
| 77 | event.Exec(Event::Time::FRAME_UPDATE); |
|---|
| 78 | cout.flush(); |
|---|
| 79 | old_time = start_time; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | // åé¡ïŒ |
|---|
| 83 | // z 軞㚠xy 軞ã®çžäºå¹²æžïŒé«éå |
|---|
| 84 | // ç§»åãããŠã£ãžããæç»ã®é«éå |
|---|
| 85 | // ãã£ãã·ã¥ |
|---|
| 86 | // æååã®äžéšã®ã¿æŽæ°ã®é«éå |
|---|
| 87 | // ãéå±€ z ã§ x ãªãé åç¡å¹åãy ãªãé åçæãã§è¯ãïŒïŒExpose |
|---|
| 88 | /* |
|---|
| 89 | Uint32 end_time = SDL_GetTicks(); |
|---|
| 90 | Uint32 delay = (end_time-start_time); |
|---|
| 91 | if(delay < 1000/framerate) SDL_Delay(1000/framerate - delay); |
|---|
| 92 | else SDL_Delay(0); |
|---|
| 93 | */ |
|---|
| 94 | SDL_Delay(0); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | void Main::SetCursor(Surface* s, const Rect& r) { |
|---|
| 99 | if (instance == NULL) return; |
|---|
| 100 | if (instance->cursor) delete instance->cursor; |
|---|
| 101 | if (s == NULL) { // ã«ãŒãœã«æ¶å» |
|---|
| 102 | instance->cursor = NULL; |
|---|
| 103 | } else if (s == DEFAULT_MOUSECURSOR) { |
|---|
| 104 | instance->cursor = NULL; |
|---|
| 105 | SDL_ShowCursor(SDL_ENABLE); |
|---|
| 106 | } else { |
|---|
| 107 | instance->cursor = new WidMouseCursor(instance->event, instance->root.root, s, r.lx, r.ty, r.width(), r.height()); |
|---|
| 108 | instance->cursor->show(); |
|---|
| 109 | SDL_ShowCursor(SDL_DISABLE); |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | } |
|---|