commit e4cde80

chld  ·  2026-10-28 12:47:59 +0000 UTC
parent 9df7c3e
remove extra includes, audio, and change err() to use show_msgbox()
1 files changed,  +9, -123
M uxn.c
M uxn.c
+9, -123
  1@@ -3,9 +3,6 @@ gcc -s -O2 -Wno-unused-result -fwrapv main.c `sdl-config --cflags --libs`
  2 exit
  3 #endif
  4 
  5-/*
  6-  stdout and stderr are ignored
  7-*/
  8 // This file is public domain.
  9 
 10 #include "SDL/SDL.h"
 11@@ -18,21 +15,6 @@ exit
 12 #include <sys/types.h>
 13 #include <time.h>
 14 #include <unistd.h>
 15-
 16-// Subprocess support
 17-#include <signal.h>
 18-#include <sys/wait.h>
 19-
 20-#ifdef __linux
 21-#include <pty.h>
 22-#include <sys/prctl.h>
 23-#endif
 24-
 25-#ifdef __NetBSD__
 26-#include <sys/ioctl.h>
 27-#include <util.h>
 28-#endif
 29-
 30 #include <stdio.h>
 31 #include <stdlib.h>
 32 #include <string.h>
 33@@ -89,7 +71,10 @@ void warn(const char *fmt, ...)
 34     va_end(args);
 35 }
 36 
 37-static void err(int e, char* str){puts(str);exit(1);}
 38+static void err(int e, char* str){
 39+  show_msgbox("Error", str);
 40+  exit(e);
 41+}
 42 
 43 static const Uint8 fontdata[]={ // font for built-in menus
 44   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x78,0x78,0x30,0x30,0x00,0x30,0x00,
 45@@ -169,13 +154,6 @@ typedef struct {
 46   Uint8 mode;
 47 } UxnFile;
 48 
 49-typedef struct {
 50-  Uint32 envtime,phase,freq;
 51-  Uint32 length,end,pos;
 52-  Uint32 env[4];
 53-  Uint8 vol[2];
 54-} UxnAudio;
 55-
 56 typedef struct {
 57   Uint8*data;
 58   Uint32 addr;
 59@@ -308,15 +286,11 @@ static Uint8 layers=0;
 60 static Uint8 palet=7;
 61 static Uint8 touch_mode=0;
 62 static Uint8 screencompat=0x03;
 63-// static int randomfd=-1;
 64 
 65 static Uint16 scriptc;
 66 static Script*scriptv;
 67 static FILE*scriptf;
 68 
 69-static Sint32*audio_option;
 70-static SDL_AudioSpec audiospec;
 71-
 72 static Uint8 joy_mask;
 73 static Uint8 joy_player[4];
 74 static Uint8 joy_hat,joy_hat_mode;
 75@@ -449,14 +423,12 @@ static void run(Uint16 pc) {
 76 static void load_rom(void) {
 77   FILE*fp=fopen(rom_name,"r");
 78   if(!fp){
 79-    show_msgbox("Error", "Cannot load program file");
 80-    exit(1);
 81+    err(1, "Cannot load program file");
 82   }
 83   memset(mem,0,MEMSIZE);
 84   fread(mem+0x0100,1,MEMSIZE-0x0100,fp);
 85   if(ferror(fp)){
 86-    show_msgbox("Error", "Cannot load program file");
 87-    exit(1);
 88+    err(1, "Cannot load program file");
 89   };
 90   fclose(fp);
 91 }
 92@@ -762,10 +734,10 @@ static void set_screen_mode(int w,int h) {
 93   scr_h=h;
 94   
 95   screen=SDL_SetVideoMode(w*zoom,h*zoom,8,SDL_SWSURFACE);
 96-  if(!screen) { show_msgbox("Error", "Cannot set screen mode"); exit(1); }
 97+  if(!screen) { err(1, "Cannot set screen mode"); }
 98   SDL_SetColors(screen,colors+palet*8,0,8);
 99   bglayer=calloc(w*h,2);
100-  if(!bglayer) { show_msgbox("Error", "Memory allocation error"); exit(1); }
101+  if(!bglayer) { err(1, "Memory allocation error"); }
102   fglayer=bglayer+w*h;
103   
104   PUT16(device[2].d + 2, scr_w);
105@@ -848,83 +820,6 @@ static void screen_out(Device*dev,Uint8 id) {
106   }
107 }
108 
109-static void audio_out(Device*dev,Uint8 id) {
110-  UxnAudio*a;
111-  Uint32 s,n;
112-  if(id!=15) return;
113-  if(use_screen) SDL_LockAudio();
114-  a=dev->aux;
115-  a->env[0]=(dev->d[8]>>4)*audio_option['e'-'a'];
116-  a->env[1]=(dev->d[8]&15)*audio_option['e'-'a'];
117-  a->env[2]=(dev->d[9]>>4)*audio_option['e'-'a'];
118-  a->env[3]=(dev->d[9]&15)*audio_option['e'-'a'];
119-  a->length=n=GET16(dev->d+10);
120-  a->pos=s=GET16(dev->d+12);
121-  a->end=s+n;
122-  if(dev->d[15]&0x80) a->length=0;
123-  a->envtime=0;
124-  a->phase=0;
125-  s=(dev->d[15]&0x7F)+audio_option['n'-'a'];
126-  s=notes[s%12]>>(10-s/12);
127-  if(n<256) s=(n*(unsigned long long)s)>>8;
128-  if(use_extension && (dev->d[6] || dev->d[7])) s=(((unsigned long long)s)*(0x10000+GET16(dev->d+6)))>>16;
129-  a->freq=s;
130-  if(audiospec.channels==1) {
131-    a->vol[0]=a->vol[1]=((dev->d[14]&15)+(dev->d[14]>>4))/2;
132-  } else {
133-    a->vol[0]=dev->d[14]>>4;
134-    a->vol[1]=dev->d[14]&15;
135-  }
136-  if(use_screen) SDL_UnlockAudio();
137-}
138-
139-static Uint32 envelope(const UxnAudio*a) {
140-  // Returns a number from 0 to 64.
141-  Uint32 t=a->envtime;
142-  if(!(a->env[0]|a->env[1]|a->env[2]|a->env[3])) return 64;
143-  if(t<a->env[0]) return ((64*t)/a->env[0])?:1;
144-  t-=a->env[0];
145-  if(t<a->env[1]) return 64-((32*t)/a->env[1]);
146-  t-=a->env[1];
147-  if(t<a->env[2]) return 32;
148-  t-=a->env[2];
149-  if(t<a->env[3]) return 32-((32*t)/a->env[3]);
150-  return 0;
151-}
152-
153-static void audio_callback(void*userdata,Uint8*stream,int len) {
154-  Uint8 c=audiospec.channels;
155-  Uint32 vol=audio_option['m'-'a']?0:audio_option['v'-'a'];
156-  Sint16*buf=(void*)stream;
157-  UxnAudio*a;
158-  Uint32 o0,o1,d,n,e;
159-  len>>=1;
160-  for(n=0;n<len;n+=c) {
161-    o0=o1=0;
162-    for(d=3;d<7;d++) {
163-      a=device[d].aux;
164-      if(a->pos>=a->end && !a->length) continue;
165-      e=envelope(a);
166-      if(!e) {
167-        a->pos=a->length=a->end=0;
168-        continue;
169-      }
170-      a->envtime++;
171-      e*=(Sint8)(mem[a->pos]^0x80);
172-      a->phase+=a->freq;
173-      if(a->phase>=0x1000000) {
174-        a->pos+=a->phase>>24;
175-        a->phase&=0xFFFFFF;
176-        while(a->pos>=a->end && a->length) a->pos-=a->length;
177-      }
178-      o0+=e*a->vol[0];
179-      o1+=e*a->vol[1];
180-    }
181-    buf[n]=(o0*vol)>>12;
182-    if(c==2) buf[n+1]=(o1*vol)>>12;
183-  }
184-}
185-
186 static void system_exp(Device *dev, Uint16 addr)
187 {
188   Uint32 src, dst;
189@@ -977,13 +872,11 @@ static void debug_screen(Uint8*s,Uint8 k) {
190         memcpy(s+80*0,buf,snprintf(buf,81,"* %s",rom_name));
191         memcpy(s+80*1,buf,snprintf(buf,81,"<c> Console: %s",use_console?"On":"Off"));
192         memcpy(s+80*2,buf,snprintf(buf,81,"<d> Debug: %s",use_debug?"On":"Off"));
193-        memcpy(s+80*3,buf,snprintf(buf,81,"<F2> Sound: %s",audio_option?(audio_option['m'-'a']?"Mute":"On"):"N/A"));
194         memcpy(s+80*4,buf,snprintf(buf,81,"<F3> Mouse: %s",use_mouse?"On":"Off"));
195         memcpy(s+80*5,buf,snprintf(buf,81,"<F4> Layers: %s",layers?(layers==1?"fg":"bg"):"fg/bg"));
196         memcpy(s+80*6,buf,snprintf(buf,81,"<F6/F7> Palette: %d",palet));
197         memcpy(s+80*7,buf,snprintf(buf,81,"<F9> Restart"));
198         memcpy(s+80*8,buf,snprintf(buf,81,"<F10> Quit"));
199-        if(audio_option) memcpy(s+80*9,buf,snprintf(buf,81,"<-/+> Volume: %d",(int)audio_option['v'-'a']));
200         memcpy(s+80*10,buf,snprintf(buf,81,"<PAUSE> Resume"));
201         memcpy(s+80*13,buf,snprintf(buf,81,"Pages: CTRL+"));
202         memcpy(s+80*14,buf,snprintf(buf,81," A:Stack   B:Return  C:Device  D:Memory  E:Files "));
203@@ -1067,12 +960,6 @@ static void debug_screen(Uint8*s,Uint8 k) {
204       case 0:
205         if(k=='c') use_console^=1;
206         if(k=='d') use_debug^=1;
207-        if(audio_option) {
208-          if((k=='-' || k==10) && audio_option['v'-'a']) --audio_option['v'-'a'];
209-          if(k=='+' || k==11) ++audio_option['v'-'a'];
210-          if(k==8) audio_option['v'-'a']/=10;
211-          if(k>='0' && k<='9') audio_option['v'-'a']=10*audio_option['v'-'a']+k-'0';
212-        }
213         break;
214       case 1: case 2:
215         if(page==1) z=&ds; else z=&rs;
216@@ -1275,7 +1162,6 @@ static int run_screen(void) {
217           case SDLK_PAGEUP: i=-scroll_size; goto scroll;
218           case SDLK_PAGEDOWN: i=scroll_size; goto scroll;
219 	case SDLK_F1: run(GET16(device[2].d));  break;
220-          case SDLK_F2: if(audio_option) audio_option['m'-'a']^=1; picture_changed=debugger; break;
221           case SDLK_F3: use_mouse^=1; set_cursor(); picture_changed=debugger; break;
222           case SDLK_F4: layers=(layers+1)%3; redraw(); break;
223           case SDLK_F5: redraw(); if(SDL_GetModState()&KMOD_RSHIFT) debug(-1); break;
224@@ -1378,7 +1264,7 @@ int main(int argc, char **argv)
225 
226   if (use_screen) {
227     if (SDL_Init(SDL_INIT_VIDEO) < 0)
228-      show_msgbox("Error", "SDL_Init failed");
229+      show_msgbox("SDL Error", "SDL_Init failed");
230     atexit(SDL_Quit);
231 
232     device[2].out = screen_out;