Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
RishiRecon
GitHub Repository: RishiRecon/exploits
Path: blob/main/misc/emulator/xnes/snem/native.c
28515 views
1
#define uint32 unsigned int
2
#define uint16 unsigned short
3
#ifdef SET_BY_SOURCE
4
#define USE_SDL
5
#define ASCII
6
#endif
7
#ifdef ASCII
8
#include "drawansi.h"
9
#endif
10
#ifdef USE_SDL
11
#include <SDL/SDL.h>
12
#include <SDL/SDL_image.h>
13
#endif
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17
18
19
#ifdef USE_SDL
20
SDL_Surface *screen;
21
Uint8 *keystates;
22
uint32 *bitmap;
23
#else
24
uint32 bitmap[288*256];
25
#endif
26
#ifdef EMCCHACK
27
#define RGB555toRGB24(t) \
28
( (((t)&0x1f)<<3) | (((t)&0x3e0)<<6) | (((t)&0x7c00)<<9) )
29
#else
30
#define RGB555toRGB24(t) \
31
( (((t)&0x1f)<<19) | (((t)&0x3e0)<<6) | (((t)&0x7c00)>>7) )
32
#endif
33
void native_hardware_init(int spcemu){
34
#ifdef USE_SDL
35
36
if( SDL_Init( SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) == -1 ) exit(-1);
37
screen = SDL_SetVideoMode( 288, 256, 32, SDL_SWSURFACE|SDL_DOUBLEBUF);
38
SDL_LockSurface(screen);
39
bitmap=(uint32 *)screen->pixels;
40
//keystates = SDL_GetKeyState( NULL );
41
#ifdef EMCCHACK
42
keystates = SDL_GetKeyboardState( NULL );
43
#else
44
atexit(SDL_Quit);
45
keystates = SDL_GetKeyState( NULL );
46
#endif
47
#endif
48
#ifdef SOUND
49
if (spcemu) initsound();
50
if (spcemu) install_int_ex(soundcheck,BPS_TO_TIMER(500));
51
#endif
52
}
53
54
uint32* native_set_joypad_state(uint32 state) __attribute__((used));
55
uint32* native_bitmap_pointer(int x, int y){
56
return (void *)(&bitmap[(y+16)*288+x+16]);
57
}
58
static int cnt=0;
59
void native_bitmap_to_screen(){
60
#ifdef USE_SDL
61
uint32 *bp=(uint32 *)native_bitmap_pointer(-16,-16);
62
int x,y;
63
for(y=0;y<16*288;y++)
64
*bp++=0;
65
bp=(uint32 *)native_bitmap_pointer(-16,224);
66
for(y=0;y<16*288;y++)
67
*bp++=0;
68
for(y=0;y<=224;y++){
69
bp=(uint32 *)native_bitmap_pointer(-32,y);
70
for(x=0;x<32;x++)
71
*bp++=0;
72
}
73
74
SDL_UnlockSurface(screen);
75
SDL_Flip(screen);
76
SDL_LockSurface(screen);
77
#endif
78
#ifdef ASCII
79
if((cnt&7)==0){
80
drawansi(256, 224, (void *)native_bitmap_pointer(0,0), 32, 288*4);
81
printf("%d\n",cnt);
82
}
83
#endif
84
85
cnt++;
86
87
}
88
void native_bitmap_clear_line(int line, uint32 color){
89
int x;
90
uint32 *bp=(uint32 *)native_bitmap_pointer(0, line);
91
for(x=0;x<256;x++)
92
bp[x]=color;
93
//memset(native_bitmap_pointer(0, line), 0, 264*2);
94
}
95
96
int native_poll_keyboard(){
97
#if defined(USE_SDL)
98
SDL_Event event;
99
while( SDL_PollEvent( &event ) ){
100
if( event.type == SDL_QUIT ) {return -1;}
101
}
102
if(keystates[SDLK_ESCAPE]) {return -1;}
103
#else
104
105
#endif
106
return 0;
107
108
}
109
#ifdef USE_SDL
110
static Uint32 my_callbackfunc(Uint32 interval, void *param)
111
{
112
113
int (*func)(void);
114
int stop_running;
115
func=(int (*)(void))param;
116
stop_running=func();
117
if(stop_running)
118
return 0;
119
#ifdef EMCCHACK
120
SDL_AddTimer( interval, my_callbackfunc, param);
121
#endif
122
return(interval);
123
}
124
#endif
125
void native_tick_callback( int (*func)(void), int fps){
126
#ifdef USE_SDL
127
SDL_AddTimer( 1000/fps, my_callbackfunc, (void *)func);
128
#endif
129
}
130
static uint32 joy1state=0x80000000;
131
uint32* native_set_joypad_state(uint32 state){
132
joy1state=state;
133
return &joy1state;
134
}
135
uint32 native_joypad_state(int num)
136
{
137
uint32 joy1=0x80000000;
138
if(num!=0) return joy1;
139
#ifdef USE_SDL
140
native_poll_keyboard();
141
if (keystates[SDLK_w]) joy1|=0x0010;
142
if (keystates[SDLK_q]) joy1|=0x0020;
143
if (keystates[SDLK_a]) joy1|=0x0040;
144
if (keystates[SDLK_s]) joy1|=0x0080;
145
if (keystates[SDLK_RIGHT]) joy1|=0x0100;
146
if (keystates[SDLK_LEFT]) joy1|=0x0200;
147
if (keystates[SDLK_DOWN]) joy1|=0x0400;
148
if (keystates[SDLK_UP]) joy1|=0x0800;
149
if (keystates[SDLK_c]) joy1|=0x1000;
150
if (keystates[SDLK_d]) joy1|=0x2000;
151
if (keystates[SDLK_z]) joy1|=0x4000;
152
if (keystates[SDLK_x]) joy1|=0x8000;
153
#else
154
/*if(cnt%120==111)
155
joy1|=(cnt&0xffff);*/
156
joy1=joy1state;
157
158
#endif
159
return joy1;
160
}
161