psx_gpu: consolidate C code, implement exnhancement asm
[pcsx_rearmed.git] / frontend / plat_sdl.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2011,2012
3  *
4  * This work is licensed under the terms of any of these licenses
5  * (at your option):
6  *  - GNU GPL, version 2 or later.
7  *  - GNU LGPL, version 2.1 or later.
8  * See the COPYING file in the top-level directory.
9  */
10
11 #include <stdio.h>
12 #include <SDL.h>
13 #include "common/input.h"
14 #include "common/in_sdl.h"
15 #include "common/menu.h"
16 #include "plugin_lib.h"
17 #include "main.h"
18 #include "plat.h"
19 #include "revision.h"
20
21 static const struct in_default_bind in_sdl_defbinds[] = {
22   { SDLK_UP,     IN_BINDTYPE_PLAYER12, DKEY_UP },
23   { SDLK_DOWN,   IN_BINDTYPE_PLAYER12, DKEY_DOWN },
24   { SDLK_LEFT,   IN_BINDTYPE_PLAYER12, DKEY_LEFT },
25   { SDLK_RIGHT,  IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
26   { SDLK_d,      IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
27   { SDLK_z,      IN_BINDTYPE_PLAYER12, DKEY_CROSS },
28   { SDLK_x,      IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
29   { SDLK_s,      IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
30   { SDLK_v,      IN_BINDTYPE_PLAYER12, DKEY_START },
31   { SDLK_c,      IN_BINDTYPE_PLAYER12, DKEY_SELECT },
32   { SDLK_w,      IN_BINDTYPE_PLAYER12, DKEY_L1 },
33   { SDLK_r,      IN_BINDTYPE_PLAYER12, DKEY_R1 },
34   { SDLK_e,      IN_BINDTYPE_PLAYER12, DKEY_L2 },
35   { SDLK_t,      IN_BINDTYPE_PLAYER12, DKEY_R2 },
36   { SDLK_ESCAPE, IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
37   { 0, 0, 0 }
38 };
39
40 static SDL_Surface *screen;
41 static void *menubg_img;
42
43 static int change_video_mode(int w, int h)
44 {
45   screen = SDL_SetVideoMode(w, h, 16, 0);
46   if (screen == NULL) {
47     fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
48     return -1;
49   }
50
51   return 0;
52 }
53
54 void plat_init(void)
55 {
56   int ret;
57
58   ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
59   if (ret != 0) {
60     fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
61     exit(1);
62   }
63
64   g_menuscreen_w = 640;
65   g_menuscreen_h = 480;
66   ret = change_video_mode(g_menuscreen_w, g_menuscreen_h);
67   if (ret != 0) {
68     ret = change_video_mode(0, 0);
69     if (ret != 0)
70       goto fail;
71
72     if (screen->w < 320 || screen->h < 240) {
73       fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
74               screen->w, screen->h);
75       goto fail;
76     }
77     g_menuscreen_w = screen->w;
78     g_menuscreen_h = screen->h;
79   }
80   SDL_WM_SetCaption("PCSX-ReARMed " REV, NULL);
81
82   menubg_img = malloc(640 * 512 * 2);
83   if (menubg_img == NULL)
84     goto fail;
85
86   in_sdl_init(in_sdl_defbinds);
87   in_probe();
88   pl_rearmed_cbs.only_16bpp = 1;
89   return;
90
91 fail:
92   SDL_Quit();
93   exit(1);
94 }
95
96 void plat_finish(void)
97 {
98   free(menubg_img);
99   menubg_img = NULL;
100   SDL_Quit();
101 }
102
103 void plat_gvideo_open(int is_pal)
104 {
105 }
106
107 void *plat_gvideo_set_mode(int *w, int *h, int *bpp)
108 {
109   change_video_mode(*w, *h);
110   return screen->pixels;
111 }
112
113 /* XXX: missing SDL_LockSurface() */
114 void *plat_gvideo_flip(void)
115 {
116   SDL_Flip(screen);
117   return screen->pixels;
118 }
119
120 void plat_gvideo_close(void)
121 {
122 }
123
124 void plat_video_menu_enter(int is_rom_loaded)
125 {
126   /* surface will be lost, must adjust pl_vout_buf for menu bg */
127   memcpy(menubg_img, screen->pixels, screen->w * screen->h * 2);
128   pl_vout_buf = menubg_img;
129
130   change_video_mode(g_menuscreen_w, g_menuscreen_h);
131 }
132
133 void plat_video_menu_begin(void)
134 {
135   SDL_LockSurface(screen);
136   g_menuscreen_ptr = screen->pixels;
137 }
138
139 void plat_video_menu_end(void)
140 {
141   SDL_UnlockSurface(screen);
142   SDL_Flip(screen);
143   g_menuscreen_ptr = NULL;
144 }
145
146 void plat_video_menu_leave(void)
147 {
148 }
149
150 /* unused stuff */
151 void *plat_prepare_screenshot(int *w, int *h, int *bpp)
152 {
153   return 0;
154 }
155
156 int plat_cpu_clock_get(void)
157 {
158   return -1;
159 }
160
161 int plat_cpu_clock_apply(int cpu_clock)
162 {
163   return -1;
164 }
165
166 int plat_get_bat_capacity(void)
167 {
168   return -1;
169 }
170
171 void plat_step_volume(int is_up)
172 {
173 }
174
175 void plat_trigger_vibrate(int is_strong)
176 {
177 }
178
179 void plat_minimize(void)
180 {
181 }
182
183 // vim:shiftwidth=2:expandtab