Free Hero Mesh

Check-in [02cc36cf71]
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:More stuff in editor and key binding
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 02cc36cf7146895a5d627c49cda3ee89c945ba40
User & Date: user on 2018-10-22 05:41:54
Other Links: manifest | tags
Context
2018-10-23
03:16
Some more display in the margin in the editor check-in: 31ac1ffcb3 user: user tags: trunk
2018-10-22
05:41
More stuff in editor and key binding check-in: 02cc36cf71 user: user tags: trunk
2018-10-20
18:09
Begin implementing the editor; also start to implement execution of key binding commands check-in: 4d0fdefe0c user: user tags: trunk
Changes

Modified bindings.c from [d28603a681] to [cc5c5337c8].

156
157
158
159
160
161
162
163






164
165
166
167
168
169
170
171
172
173
174

175
176
177
178











179
180

























181
182
183
184
185
156
157
158
159
160
161
162

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224







-
+
+
+
+
+
+










-
+




+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+





  if((m&KMOD_NUM) && kb->m[i|MOD_NUMLOCK].cmd) i|=MOD_NUMLOCK;
  return kb->m+i;
}

int exec_key_binding(SDL_Event*ev,int editing,int x,int y,int(*cb)(int prev,int cmd,int number,int argc,sqlite3_stmt*args,void*aux),void*aux) {
  const UserCommand*cmd=find_key_binding(ev,editing);
  int prev=0;
  int i;
  int i,j,k;
  const char*name;
  if(ev->type==SDL_MOUSEBUTTONDOWN && !x && !y && ev->button.x>=left_margin) {
    x=(ev->button.x-left_margin)/picture_size+1;
    y=ev->button.y/picture_size+1;
  }
  switch(cmd->cmd) {
    case 0:
      return 0;
    case '^':
      return cb(0,cmd->n*'\0\1'+'^\0',0,0,0,aux);
    case '=': case '-': case '+':
      return cb(0,cmd->cmd*'\1\0'+'\0 ',cmd->n,0,0,aux);
    case '\'':
      return cb(0,'\' ',cmd->n,0,0,aux);
    case '!':
      system(cmd->txt);
      i=system(cmd->txt);
      return 0;
    case 's':
      sqlite3_reset(cmd->stmt);
      sqlite3_clear_bindings(cmd->stmt);
      if(sqlite3_bind_parameter_count(cmd->stmt)) {
        for(i=sqlite3_bind_parameter_count(cmd->stmt);i;--i) if(name=sqlite3_bind_parameter_name(cmd->stmt,i)) {
          if(*name=='$') {
            if(!sqlite3_stricmp(name+1,"X")) {
              if(x) sqlite3_bind_int(cmd->stmt,i,x);
            } else if(!sqlite3_stricmp(name+1,"Y")) {
              if(y) sqlite3_bind_int(cmd->stmt,i,y);
            } else if(!sqlite3_stricmp(name+1,"LEVEL")) {
              sqlite3_bind_int(cmd->stmt,i,level_ord);
            } else if(!sqlite3_stricmp(name+1,"LEVEL_ID")) {
              sqlite3_bind_int(cmd->stmt,i,level_id);
      
      break;
            }
          } else if(*name==':') {
            name=screen_prompt(name);
            if(!name) return 0;
            sqlite3_bind_text(cmd->stmt,i,name,-1,SQLITE_TRANSIENT);
          }
        }
      }
      while((i=sqlite3_step(cmd->stmt))==SQLITE_ROW) {
        if(i=sqlite3_data_count(cmd->stmt)) {
          j=i>1?sqlite3_column_int(cmd->stmt,1):0;
          if((name=sqlite3_column_text(cmd->stmt,0)) && *name) {
            k=name[0]*'\1\0'+name[1]*'\0\1';
            prev=cb(prev,k,j,i,cmd->stmt,aux);
            if(prev<0) {
              i=SQLITE_DONE;
              break;
            }
          }
        }
      }
      sqlite3_reset(cmd->stmt);
      sqlite3_clear_bindings(cmd->stmt);
      if(i!=SQLITE_DONE) fprintf(stderr,"SQL error in key binding: %s\n",sqlite3_errmsg(userdb)?:"unknown error");
      return prev;
    default:
      fprintf(stderr,"Confusion in exec_key_binding()\n");
      return 0;
  }
}

Modified cursorshapes.h from [3eb0c464ce] to [ffa250dfdb].

73
74
75
76
77
78
79

80
81
82
83
84
85
86
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87







+







#define XC_trek 142
#define XC_ul_angle 144
#define XC_umbrella 146
#define XC_ur_angle 148
#define XC_watch 150
#define XC_xterm 152
#endif
#ifdef HEROMESH_MAIN
static const Uint8 cursorimg[]={
0,0,112,14,120,30,124,62,62,124,31,248,15,240,7,224,7,224,15,240,31,248,62,124,124,62,120,30,112,14,0,0,
240,15,248,31,252,63,254,127,127,254,63,252,31,248,15,240,15,240,31,248,63,252,127,254,254,127,252,63,248,31,240,15,
0,0,0,6,0,30,0,124,1,252,7,248,31,248,1,240,3,240,7,96,14,96,28,64,56,64,112,0,32,0,0,0,
0,7,0,31,0,127,1,254,7,254,31,252,63,252,63,248,7,248,15,240,31,240,62,224,124,224,248,64,112,0,32,0,
0,0,127,128,0,0,127,128,12,0,12,0,12,0,12,0,45,0,30,0,12,0,0,0,0,0,0,0,0,0,0,0,
255,192,255,192,255,192,255,192,255,192,30,0,30,0,127,128,127,128,127,128,63,0,30,0,0,0,0,0,0,0,0,0,
307
308
309
310
311
312
313

308
309
310
311
312
313
314
315







+
64,
17,
130,
161,
249,
72,
};
#endif

Modified edit.c from [574b134f31] to [47203e30e6].

1
2

3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28



29
30
31
32
33
34
35
1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

-
+











+















+
+
+







#if 0
gcc ${CFLAGS:--s -O2} -c edit.c `sdl-config --cflags`
gcc ${CFLAGS:--s -O2} -c -Wno-multichar edit.c `sdl-config --cflags`
exit
#endif

#include "SDL.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "smallxrm.h"
#include "heromesh.h"
#include "quarks.h"
#include "cursorshapes.h"

static void redraw_editor(void) {
  SDL_Rect r;
  int x,y;
  r.x=r.y=0;
  r.h=screen->h;
  r.w=left_margin;
  SDL_FillRect(screen,&r,0xF0);
  r.x=left_margin-1;
  r.w=1;
  SDL_FillRect(screen,&r,0xF7);
  r.x=left_margin;
  r.w=screen->w-r.x;
  SDL_FillRect(screen,&r,back_color);
  for(x=1;x<=pfwidth;x++) for(y=1;y<=pfheight;y++) draw_cell(x,y);
  SDL_LockSurface(screen);
  draw_text(0,0,"EDIT",0xF0,0xF7);
  SDL_UnlockSurface(screen);
  SDL_Flip(screen);
}

static void set_caption(void) {
  const char*r;
  char*s;
  sqlite3_str*m;
43
44
45
46
47
48
49











50
51
52

53
54
55
56
57
58
59
60
61
62















63
64
65
66
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97







+
+
+
+
+
+
+
+
+
+
+



+










+
+
+
+
+
+
+
+
+
+
+
+
+
+
+




    sqlite3_str_appendall(m,basefilename);
    sqlite3_str_appendall(m,r+c+1);
  }
  s=sqlite3_str_finish(m);
  if(s) SDL_WM_SetCaption(s,s); else SDL_WM_SetCaption("Free Hero Mesh","Free Hero Mesh");
  sqlite3_free(s);
}

static int editor_command(int prev,int cmd,int number,int argc,sqlite3_stmt*args,void*aux) {
  switch(cmd) {
    case '^P': // Play
      return -2;
    case '^Q': // Quit
      return -1;
    default:
      return prev;
  }
}

void run_editor(void) {
  SDL_Event ev;
  int i;
  set_caption();
  load_level(level_id);
  redraw_editor();
  while(SDL_WaitEvent(&ev)) {
    switch(ev.type) {
      case SDL_VIDEOEXPOSE:
        redraw_editor();
        break;
      case SDL_QUIT:
        exit(0);
        break;
      case SDL_MOUSEMOTION:
        set_cursor(ev.motion.x<left_margin?XC_arrow:XC_tcross);
        break;
      case SDL_MOUSEBUTTONDOWN:
        
        // fallthrough
      case SDL_KEYDOWN:
        i=exec_key_binding(&ev,1,0,0,editor_command,0);
        if(i==-1) exit(0);
        if(i==-2) {
          main_options['e']=0;
          return;
        }
        redraw_editor();
        break;
    }
  }
}

Modified heromesh.h from [6365c4e997] to [5ea05aa2a2].

83
84
85
86
87
88
89

90
91
92
93
94
95
96
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97







+







extern SDL_Surface*screen;
extern Uint16 picture_size;
extern int left_margin;

void draw_picture(int x,int y,Uint16 img);
void draw_text(int x,int y,const unsigned char*t,int bg,int fg);
void draw_cell(int x,int y);
const char*screen_prompt(const char*txt);
void load_pictures(void);

// == class ==

#define CF_PLAYER 0x01
#define CF_INPUT 0x02
#define CF_COMPATIBLE 0x04
149
150
151
152
153
154
155

156
157
158
159
160
161
162
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164







+







    sqlite3_stmt*stmt;
    const char*txt;
  };
} UserCommand;

void load_key_bindings(void);
const UserCommand*find_key_binding(SDL_Event*ev,int editing);
int exec_key_binding(SDL_Event*ev,int editing,int x,int y,int(*cb)(int prev,int cmd,int number,int argc,sqlite3_stmt*args,void*aux),void*aux);

// == function ==

void init_sql_functions(sqlite3_int64*ptr0,sqlite3_int64*ptr1);

// == exec ==

Modified main.c from [281782dbc2] to [7611edaf9e].

607
608
609
610
611
612
613



614
615
616
617
618
619
620
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623







+
+
+







          goto keytest;
        case SDLK_p:
          sqlite3_exec(userdb,"SELECT * FROM `PICTURES`;",test_sql_callback,0,0);
          break;
        case SDLK_q:
          exit(0);
          break;
        case SDLK_t:
          puts(screen_prompt("Testing screen_prompt()")?:"No output.");
          break;
      }
      break;
    case SDL_MOUSEBUTTONDOWN:
      draw_picture(ev.button.x,ev.button.y,n);
      SDL_Flip(screen);
      break;
    case SDL_QUIT:

Modified picture.c from [51eec2ef53] to [7850e1bb3d].

14
15
16
17
18
19
20

21
22
23
24
25
26
27
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28







+







#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "smallxrm.h"
#include "pcfont.h"
#include "quarks.h"
#include "heromesh.h"
#include "cursorshapes.h"

SDL_Surface*screen;
Uint16 picture_size;
int left_margin;

static SDL_Surface*picts;
static Uint8*curpic;
128
129
130
131
132
133
134



































































135
136
137
138
139
140
141
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







      ++f;
    }
    t++;
    if(!--len) return;
    pix+=8;
  }
}

const char*screen_prompt(const char*txt) {
  static char*t=0;
  int n=0;
  SDL_Rect r={0,0,screen->w,16};
  int m=r.w>>3;
  SDL_Event ev;
  if(!t) {
    t=malloc(m+2);
    if(!t) fatal("Allocation failed\n");
  }
  *t=0;
  SDL_FillRect(screen,&r,0xF1);
  r.y=16;
  r.h=1;
  SDL_FillRect(screen,&r,0xF8);
  SDL_LockSurface(screen);
  draw_text(0,0,txt,0xF1,0xFE);
  draw_text(0,8,"\xB1",0xF1,0xFB);
  SDL_UnlockSurface(screen);
  set_cursor(XC_iron_cross);
  SDL_Flip(screen);
  r.y=8;
  r.h=8;
  while(SDL_WaitEvent(&ev)) {
    switch(ev.type) {
      case SDL_QUIT:
        SDL_PushEvent(&ev);
        return 0;
      case SDL_KEYDOWN:
        SDL_FillRect(screen,&r,0xF1);
        switch(ev.key.keysym.sym) {
          case SDLK_RETURN: case SDLK_KP_ENTER:
            r.y=0;
            r.h=17;
            SDL_FillRect(screen,&r,0xF0);
            t[n]=0;
            return t;
          case SDLK_BACKSPACE: case SDLK_DELETE:
            if(n) t[n--]=0;
            break;
          case SDLK_CLEAR:
            t[n=0]=0;
            break;
          default:
            if(ev.key.keysym.sym==SDLK_u && (ev.key.keysym.mod&KMOD_CTRL)) {
              t[n=0]=0;
            } else if(ev.key.keysym.sym==SDLK_c && (ev.key.keysym.mod&KMOD_CTRL)) {
              r.y=0;
              r.h=17;
              SDL_FillRect(screen,&r,0xF0);
              return 0;
            } else if(n<m && ev.key.keysym.unicode<127 && ev.key.keysym.unicode>=32 && !(ev.key.keysym.mod&KMOD_CTRL)) {
              t[n++]=ev.key.keysym.unicode;
              t[n]=0;
            }
        }
        t[n]=0;
        SDL_FillRect(screen,&r,0xF1);
        SDL_LockSurface(screen);
        draw_text(0,8,t,0xF1,0xFF);
        draw_text(n<<3,8,"\xB1",0xF1,0xFB);
        SDL_UnlockSurface(screen);
        SDL_Flip(screen);
    }
  }
}

static Uint16 decide_picture_size(int nwantsize,const Uint8*wantsize,const Uint16*havesize,int n) {
  int i,j;
  if(!nwantsize) fatal("Unable to determine what picture size is wanted\n");
  for(i=0;i<nwantsize;i++) if(havesize[j=wantsize[i]]==n) return j;
  for(i=*wantsize;i;--i) if(havesize[i]) return i;
  fatal("Unable to determine what picture size is wanted\n");
353
354
355
356
357
358
359

360
361
362
421
422
423
424
425
426
427
428
429
430
431







+



  if(v=xrm_get_resource(resourcedb,optionquery,optionquery,2)) {
    w=strtol(v,(void*)&v,10);
    h=strtol(v,0,10);
    SDL_EnableKeyRepeat(w,h);
  } else {
    SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
  }
  SDL_EnableUNICODE(1);
  optionquery[1]=Q_margin;
  left_margin=strtol(xrm_get_resource(resourcedb,optionquery,optionquery,2)?:"65",0,10);
}