Free Hero Mesh

Check-in [b43c838ca3]
Login
This is a mirror of the main repository for Free Hero Mesh. New tickets and changes will not be accepted at this mirror.
Overview
Comment:Implement loading standard sounds, and working in sound test menu. (Free replacements for standard sounds are not yet available, though.)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b43c838ca36496e2ebcca1384d46d8cb834bc86c
User & Date: user on 2022-01-21 05:17:46
Other Links: manifest | tags
Context
2022-01-25
06:31
Implement (attribute userflags) syntax, in order to access bit patterns of user flags directly in class codes (e.g. so that they can be passed to subroutines, etc). check-in: a724f44255 user: user tags: trunk
2022-01-21
05:17
Implement loading standard sounds, and working in sound test menu. (Free replacements for standard sounds are not yet available, though.) check-in: b43c838ca3 user: user tags: trunk
2022-01-20
06:37
Explain the "Attempt to use a nonexistent object" error when attempting to move a Sliding Door after one side has been destroyed. check-in: 1977494fc3 user: user tags: trunk
Changes

Modified config.doc from [6c9a4775e5] to [dfb6c74832].

31
32
33
34
35
36
37
38
39
40
41
42
43




44
45


46
47
48
49
50
51
52
.audio.mmlTuning
  The MML tuning of the A4 note, in Hz. The default is 440 Hz, but some
  people do not like that tuning, so you can change it. This setting only
  affects 24-TET sounds, and not overtone-based sounds.

.audio.mmlVolume
  MML volume, from 0 to 1; fractions are allowed. The default value is
  approximately one third.

.audio.rate
  Sample rate to use for audio, in hertz. In order for audio to work at
  all, this value must be set.





.audio.waveVolume
  Wave volume, from 0 to 1; fractions are allowed. The default value is 1.



.autoSave
  If true, saves changes to the level and solution files when the program
  terminates. If false (default), they are only saved to the user cache
  database; to copy the changes to the level and solution files, you must
  use the .u command in SQL mode (-x), or use the -f switch.








|





>
>
>
>


>
>







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
.audio.mmlTuning
  The MML tuning of the A4 note, in Hz. The default is 440 Hz, but some
  people do not like that tuning, so you can change it. This setting only
  affects 24-TET sounds, and not overtone-based sounds.

.audio.mmlVolume
  MML volume, from 0 to 1; fractions are allowed. The default value is
  approximately one third. Set it to zero to disable MML sounds.

.audio.rate
  Sample rate to use for audio, in hertz. In order for audio to work at
  all, this value must be set.

.audio.standardSounds
  The name of the Hamster archive file containing the standard sounds. If
  not specified, then the standard sounds won't work.

.audio.waveVolume
  Wave volume, from 0 to 1; fractions are allowed. The default value is 1.
  If this is zero, then it will not load any sound effects, but MML sounds
  will still work (if .audio.buffer and .audio.rate are set correctly).

.autoSave
  If true, saves changes to the level and solution files when the program
  terminates. If false (default), they are only saved to the user cache
  database; to copy the changes to the level and solution files, you must
  use the .u command in SQL mode (-x), or use the -f switch.

Modified sound.c from [c480308e9b] to [8e65d6bcfc].

10
11
12
13
14
15
16


17
18
19
20
21
22
23
#include <string.h>
#include "sqlite3.h"
#include "smallxrm.h"
#include "quarks.h"
#include "heromesh.h"
#include "cursorshapes.h"



typedef struct {
  Uint8*data;
  Uint32 len; // length in bytes
} WaveSound;

static Uint8 sound_on;
static Sint16 mmlvolume=10000;







>
>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string.h>
#include "sqlite3.h"
#include "smallxrm.h"
#include "quarks.h"
#include "heromesh.h"
#include "cursorshapes.h"

#define N_STANDARD_SOUNDS 50

typedef struct {
  Uint8*data;
  Uint32 len; // length in bytes
} WaveSound;

static Uint8 sound_on;
static Sint16 mmlvolume=10000;
167
168
169
170
171
172
173

174


175
176
177
178
179
180
181
182
    }
    if(!fp) return;
  } else {
    optionquery[2]=Q_standardSounds;
    v=xrm_get_resource(resourcedb,optionquery,optionquery,3);
    if(!v) return;
    fp=fopen(v,"r");

    if(!fp) fatal("Cannot open standard sounds file (%m)\n");


    nstandardsounds=50;
    standardsounds=malloc(nstandardsounds*sizeof(WaveSound));
    if(!standardsounds) fatal("Allocation failed\n");
    for(i=0;i<nstandardsounds;i++) standardsounds[i].data=0,standardsounds[i].len=0;
  }
  nam=malloc(256);
  for(;;) {
    for(i=0;;) {







>
|
>
>
|







169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    }
    if(!fp) return;
  } else {
    optionquery[2]=Q_standardSounds;
    v=xrm_get_resource(resourcedb,optionquery,optionquery,3);
    if(!v) return;
    fp=fopen(v,"r");
    if(!fp) {
      fprintf(stderr,"Cannot open standard sounds file (%m)\n");
      return;
    }
    nstandardsounds=N_STANDARD_SOUNDS+N_MESSAGES;
    standardsounds=malloc(nstandardsounds*sizeof(WaveSound));
    if(!standardsounds) fatal("Allocation failed\n");
    for(i=0;i<nstandardsounds;i++) standardsounds[i].data=0,standardsounds[i].len=0;
  }
  nam=malloc(256);
  for(;;) {
    for(i=0;;) {
199
200
201
202
203
204
205







206

207
208
209
210
211
212
213

214
215
216
217
218
219
220
        if(!usersounds || !user_sound_names) fatal("Allocation failed\n");
        user_sound_names[i]=strdup(nam);
        if(!user_sound_names[i]) fatal("Allocation failed\n");
        ws=usersounds+i;
        ws->data=0;
        ws->len=0;
      } else {







        //TODO: Implement standard sounds.

      }
      if(j=='A') {
        load_sound(fp,l_offset,l_size,ws);
      } else {
        //TODO: Compressed sounds.
      }
    }

    fseek(fp,l_offset+l_size,SEEK_SET);
  }
  done:
  fclose(fp);
  free(nam);
}








>
>
>
>
>
>
>
|
>







>







204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
        if(!usersounds || !user_sound_names) fatal("Allocation failed\n");
        user_sound_names[i]=strdup(nam);
        if(!user_sound_names[i]) fatal("Allocation failed\n");
        ws=usersounds+i;
        ws->data=0;
        ws->len=0;
      } else {
        for(i=0;i<N_STANDARD_SOUNDS;i++) if(!sqlite3_stricmp(nam,standard_sound_names[i])) goto found;
        for(i=0;i<N_MESSAGES;i++) if(!sqlite3_stricmp(nam,standard_message_names[i])) {
          i+=N_STANDARD_SOUNDS;
          goto found;
        }
        goto notfound;
        found:
        ws=standardsounds+i;
        if(ws->data) goto notfound;
      }
      if(j=='A') {
        load_sound(fp,l_offset,l_size,ws);
      } else {
        //TODO: Compressed sounds.
      }
    }
    notfound:
    fseek(fp,l_offset+l_size,SEEK_SET);
  }
  done:
  fclose(fp);
  free(nam);
}

248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
  if(v=xrm_get_resource(resourcedb,optionquery,optionquery,3)) {
    needs_amplify=1;
    wavevolume=strtod(v,0);
  }
  optionquery[2]=Q_mmlVolume;
  if(v=xrm_get_resource(resourcedb,optionquery,optionquery,3)) mmlvolume=fmin(strtod(v,0)*32767.0,32767.0);
  if(wavevolume>0.00001) {
    //load_sound_set(0); // Standard sounds
    load_sound_set(1); // User sounds
  }
  if(mmlvolume) {
    mmltuning=malloc(256*sizeof(Uint32));
    if(!mmltuning) fatal("Allocation failed\n");
    optionquery[2]=Q_mmlTuning;
    if(v=xrm_get_resource(resourcedb,optionquery,optionquery,3)) f=strtod(v,0); else f=440.0;







|







262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  if(v=xrm_get_resource(resourcedb,optionquery,optionquery,3)) {
    needs_amplify=1;
    wavevolume=strtod(v,0);
  }
  optionquery[2]=Q_mmlVolume;
  if(v=xrm_get_resource(resourcedb,optionquery,optionquery,3)) mmlvolume=fmin(strtod(v,0)*32767.0,32767.0);
  if(wavevolume>0.00001) {
    load_sound_set(0); // Standard sounds
    load_sound_set(1); // User sounds
  }
  if(mmlvolume) {
    mmltuning=malloc(256*sizeof(Uint32));
    if(!mmltuning) fatal("Allocation failed\n");
    optionquery[2]=Q_mmlTuning;
    if(v=xrm_get_resource(resourcedb,optionquery,optionquery,3)) f=strtod(v,0); else f=440.0;
337
338
339
340
341
342
343



344
345
346
347
348
349
350
  const unsigned char*s;
  if(!sound_on) return;
  if(!v2.t && !v2.u && (mmlpos || wavesound)) return;
  SDL_LockAudio();
  wavesound=0;
  mmlpos=0;
  switch(v1.t) {



    case TY_SOUND:
      if(v1.u<nstandardsounds) {
        wavesound=standardsounds[v1.u].data;
        wavelen=standardsounds[v1.u].len;
      }
      break;
    case TY_USOUND:







>
>
>







351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  const unsigned char*s;
  if(!sound_on) return;
  if(!v2.t && !v2.u && (mmlpos || wavesound)) return;
  SDL_LockAudio();
  wavesound=0;
  mmlpos=0;
  switch(v1.t) {
    case TY_MESSAGE:
      v1.u+=N_STANDARD_SOUNDS;
      //fallthrough
    case TY_SOUND:
      if(v1.u<nstandardsounds) {
        wavesound=standardsounds[v1.u].data;
        wavelen=standardsounds[v1.u].len;
      }
      break;
    case TY_USOUND:
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449

450
451




452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471


472
473
474
475
476
477
478
  char buf[256];
  if(main_options['T'] && main_options['v']) {
    if(mmltuning) printf("mmltempo=%d; mmlvolume=%d; mmltuning[96]=%d\n",(int)mmltempo,(int)mmlvolume,(int)mmltuning[96]);
    for(i=0;i<nusersounds;i++) printf("%d: %s (ptr=%p, len=%d bytes)\n",i,user_sound_names[i],usersounds[i].data,usersounds[i].len);
    fflush(stdout);
  }
  if(!screen) return;
  nitems=nusersounds+8;
  columns=(screen->w-16)/240?:1;
  scrmax=(nitems+columns-1)/columns;
  set_cursor(XC_arrow);
  redraw:
  SDL_FillRect(screen,0,0x02);
  r.x=r.y=0;
  r.w=screen->w;
  r.h=8;
  SDL_FillRect(screen,&r,0xF7);
  SDL_LockSurface(screen);
  draw_text(0,0,"  <F1> Mute  <F2> Stop  <F3> Status  <ESC> Cancel",0xF7,0xF0);
  if(SDL_GetAudioStatus()==SDL_AUDIO_PLAYING) draw_text(0,0,"\x0E",0xF7,0xF1);
  for(i=scroll*columns,x=0,y=8;i<nitems;i++) {
    if(y>screen->h-24) break;
    if(i<nstandardsounds) {
      k=0xF9;
      snprintf(buf,29,"0"); //TODO
    } else if(i<nstandardsounds+nusersounds) {
      k=0xFA;
      snprintf(buf,29,"%s",user_sound_names[i-nstandardsounds]);
    } else {
      k=0xFB;
      snprintf(buf,29,"TEST %d",i-nstandardsounds-nusersounds);
    }
    r.x=x*240+20; r.y=y+4;
    r.w=232; r.h=16;
    SDL_FillRect(screen,&r,k);
    draw_text(r.x+4,r.y+4,buf,k,0xF0);
    if(++x==columns) x=0,y+=24;
  }
  SDL_UnlockSurface(screen);
  r.x=r.w=0; r.y=8; r.h=screen->h-8;
  scrollbar(&scroll,screen->h/8-1,scrmax,0,&r);
  SDL_Flip(screen);
  while(SDL_WaitEvent(&ev)) {
    if(ev.type!=SDL_VIDEOEXPOSE && scrollbar(&scroll,screen->h/8-1,scrmax,&ev,&r)) goto redraw;
    switch(ev.type) {
      case SDL_MOUSEMOTION:
        x=ev.button.x-16; y=ev.button.y-8;
        if(x<0 || y<0) goto arrow;
        i=x/240+columns*(y/24); x%=240; y%=24;
        if(x<4 || x>236 || y<4 || y>20 || i<0 || i>=nitems) goto arrow;
        set_cursor(XC_hand1);
        break;
        arrow: set_cursor(XC_arrow);
        break;
      case SDL_MOUSEBUTTONDOWN:
        x=ev.button.x-16; y=ev.button.y-8;
        if(x<0 || y<0) break;
        i=x/240+columns*(y/24); x%=240; y%=24;
        if(x<4 || x>236 || y<4 || y>20 || i<0 || i>=nitems) break;
        if(i<nstandardsounds) {

          v.t=TY_SOUND;
          v.u=i;




        } else if(i<nstandardsounds+nusersounds) {
          v.t=TY_USOUND;
          v.u=i-nstandardsounds;
        } else {
          v.t=TY_FOR;
          v.u=i-nstandardsounds-nusersounds;
        }
        set_sound_effect(v,NVALUE(ev.button.button-1));
        break;
      case SDL_KEYDOWN:
        switch(ev.key.keysym.sym) {
          case SDLK_ESCAPE: return;
          case SDLK_F1: set_sound_on(SDL_GetAudioStatus()==SDL_AUDIO_PAUSED); goto redraw;
          case SDLK_F2: set_sound_effect(NVALUE(0),NVALUE(1)); break;
          case SDLK_F3:
            snprintf(buf,255,"Sample rate: %d Hz\nBuffer size: %d samples (%d bytes)\nStatus: %d\nWave queue: %d bytes\nMML position: %d"
             ,(int)spec.freq,(int)spec.samples,(int)spec.size,(int)SDL_GetAudioStatus(),(int)wavelen,(int)mmlpos);
            modal_draw_popup(buf);
            goto redraw;
          case SDLK_HOME: case SDLK_KP7: scroll=0; goto redraw;


        }
        break;
      case SDL_VIDEOEXPOSE: goto redraw;
      case SDL_QUIT: SDL_PushEvent(&ev); return;
    }
  }
}







|















|
|















|


|













|


>
|
|
>
>
>
>




















>
>







407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
  char buf[256];
  if(main_options['T'] && main_options['v']) {
    if(mmltuning) printf("mmltempo=%d; mmlvolume=%d; mmltuning[96]=%d\n",(int)mmltempo,(int)mmlvolume,(int)mmltuning[96]);
    for(i=0;i<nusersounds;i++) printf("%d: %s (ptr=%p, len=%d bytes)\n",i,user_sound_names[i],usersounds[i].data,usersounds[i].len);
    fflush(stdout);
  }
  if(!screen) return;
  nitems=nstandardsounds+nusersounds+8;
  columns=(screen->w-16)/240?:1;
  scrmax=(nitems+columns-1)/columns;
  set_cursor(XC_arrow);
  redraw:
  SDL_FillRect(screen,0,0x02);
  r.x=r.y=0;
  r.w=screen->w;
  r.h=8;
  SDL_FillRect(screen,&r,0xF7);
  SDL_LockSurface(screen);
  draw_text(0,0,"  <F1> Mute  <F2> Stop  <F3> Status  <ESC> Cancel",0xF7,0xF0);
  if(SDL_GetAudioStatus()==SDL_AUDIO_PLAYING) draw_text(0,0,"\x0E",0xF7,0xF1);
  for(i=scroll*columns,x=0,y=8;i<nitems;i++) {
    if(y>screen->h-24) break;
    if(i<nstandardsounds) {
      k=standardsounds[i].data?0xF9:0xF8;
      snprintf(buf,29,"%s",i<N_STANDARD_SOUNDS?standard_sound_names[i]:standard_message_names[i-N_STANDARD_SOUNDS]); //TODO
    } else if(i<nstandardsounds+nusersounds) {
      k=0xFA;
      snprintf(buf,29,"%s",user_sound_names[i-nstandardsounds]);
    } else {
      k=0xFB;
      snprintf(buf,29,"TEST %d",i-nstandardsounds-nusersounds);
    }
    r.x=x*240+20; r.y=y+4;
    r.w=232; r.h=16;
    SDL_FillRect(screen,&r,k);
    draw_text(r.x+4,r.y+4,buf,k,0xF0);
    if(++x==columns) x=0,y+=24;
  }
  SDL_UnlockSurface(screen);
  r.x=r.w=0; r.y=8; r.h=screen->h-8;
  scrollbar(&scroll,screen->h/24-1,scrmax,0,&r);
  SDL_Flip(screen);
  while(SDL_WaitEvent(&ev)) {
    if(ev.type!=SDL_VIDEOEXPOSE && scrollbar(&scroll,screen->h/24-1,scrmax,&ev,&r)) goto redraw;
    switch(ev.type) {
      case SDL_MOUSEMOTION:
        x=ev.button.x-16; y=ev.button.y-8;
        if(x<0 || y<0) goto arrow;
        i=x/240+columns*(y/24); x%=240; y%=24;
        if(x<4 || x>236 || y<4 || y>20 || i<0 || i>=nitems) goto arrow;
        set_cursor(XC_hand1);
        break;
        arrow: set_cursor(XC_arrow);
        break;
      case SDL_MOUSEBUTTONDOWN:
        x=ev.button.x-16; y=ev.button.y-8;
        if(x<0 || y<0) break;
        i=x/240+columns*(y/24)+scroll*columns; x%=240; y%=24;
        if(x<4 || x>236 || y<4 || y>20 || i<0 || i>=nitems) break;
        if(i<nstandardsounds) {
          if(i<N_STANDARD_SOUNDS) {
            v.t=TY_SOUND;
            v.u=i;
          } else {
            v.t=TY_MESSAGE;
            v.u=i-N_STANDARD_SOUNDS;
          }
        } else if(i<nstandardsounds+nusersounds) {
          v.t=TY_USOUND;
          v.u=i-nstandardsounds;
        } else {
          v.t=TY_FOR;
          v.u=i-nstandardsounds-nusersounds;
        }
        set_sound_effect(v,NVALUE(ev.button.button-1));
        break;
      case SDL_KEYDOWN:
        switch(ev.key.keysym.sym) {
          case SDLK_ESCAPE: return;
          case SDLK_F1: set_sound_on(SDL_GetAudioStatus()==SDL_AUDIO_PAUSED); goto redraw;
          case SDLK_F2: set_sound_effect(NVALUE(0),NVALUE(1)); break;
          case SDLK_F3:
            snprintf(buf,255,"Sample rate: %d Hz\nBuffer size: %d samples (%d bytes)\nStatus: %d\nWave queue: %d bytes\nMML position: %d"
             ,(int)spec.freq,(int)spec.samples,(int)spec.size,(int)SDL_GetAudioStatus(),(int)wavelen,(int)mmlpos);
            modal_draw_popup(buf);
            goto redraw;
          case SDLK_HOME: case SDLK_KP7: scroll=0; goto redraw;
          case SDLK_UP: case SDLK_KP8: if(scroll) --scroll; goto redraw;
          case SDLK_DOWN: case SDLK_KP2: ++scroll; goto redraw;
        }
        break;
      case SDL_VIDEOEXPOSE: goto redraw;
      case SDL_QUIT: SDL_PushEvent(&ev); return;
    }
  }
}