home *** CD-ROM | disk | FTP | other *** search
/ hobbes.nmsu.edu 2008 / 2008-06-02_hobbes.nmsu.edu.zip / new / scummc-0.2.0-os2.zip / ScummC / src / scvm.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-01-14  |  12.8 KB  |  547 lines

  1. /* ScummC
  2.  * Copyright (C) 2006  Alban Bedel
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17.  *
  18.  */
  19.  
  20. /// @defgroup scvm SCVM
  21. /**
  22.  * @file scvm.h
  23.  * @ingroup scvm
  24.  * @brief A primitive SCUMM VM implementation.
  25.  */
  26.  
  27. #define SCVM_ARRAY_BIT    0
  28. #define SCVM_ARRAY_NIBBLE 1
  29. #define SCVM_ARRAY_BYTE   2
  30. #define SCVM_ARRAY_WORD   3
  31.  
  32. #define SCVM_MAX_ZPLANE  16
  33.  
  34. typedef struct scvm_array {
  35.   unsigned type; // var type
  36.   unsigned size; // num elements
  37.   unsigned line_size; // for 2 dimension arrays
  38.   union {
  39.     uint16_t *word;
  40.     uint8_t *byte;
  41.   } data;
  42. } scvm_array_t;
  43.  
  44. /// Number of colors in a palette
  45. #define SCVM_PALETTE_SIZE 256
  46.  
  47. typedef struct scvm_color {
  48.   uint8_t r,g,b;
  49. } scvm_color_t;
  50.  
  51. typedef scvm_color_t scvm_palette_t[SCVM_PALETTE_SIZE];
  52.  
  53. typedef struct scvm_image {
  54.   uint8_t* data;
  55.   uint8_t** zplane;  
  56.   unsigned have_trans;
  57. } scvm_image_t;
  58.  
  59. typedef struct scvm_object_pdata {
  60.   unsigned klass;
  61.   unsigned owner;
  62.   unsigned state;
  63.   char* name;
  64. } scvm_object_pdata_t;
  65.  
  66. #define SCVM_OBJ_OBIM 1
  67. #define SCVM_OBJ_OBCD 2
  68.  
  69. typedef struct scvm_object scvm_object_t;
  70.  
  71. struct scvm_object {
  72.   unsigned id,loaded;
  73.   unsigned num_image;
  74.   unsigned num_zplane;
  75.   unsigned width,height;
  76.   int x,y;
  77.   unsigned num_hotspot;
  78.   int* hotspot;
  79.   scvm_image_t* image;
  80.   unsigned flags;
  81.   scvm_object_t* parent;
  82.   unsigned actor_dir;
  83.   unsigned* verb_entries;
  84.   unsigned num_verb_entries;
  85.   scvm_script_t* script;
  86.   // presistent object data
  87.   scvm_object_pdata_t* pdata;
  88. };
  89.  
  90. int scvm_get_object_position(scvm_t* vm, unsigned id, int* x, int* y);
  91.  
  92.  
  93. typedef struct scvm_cycle {
  94.   unsigned id;
  95.   unsigned delay;
  96.   unsigned flags;
  97.   unsigned start,end;
  98.   unsigned counter;
  99. } scvm_cycle_t;
  100.  
  101. typedef struct scvm_room {
  102.   unsigned id;
  103.   // graphics
  104.   unsigned width,height;  
  105.   unsigned num_zplane;
  106.   uint8_t* zplane[SCVM_MAX_ZPLANE];
  107.   scvm_image_t image;
  108.   unsigned num_palette;
  109.   scvm_palette_t* palette;
  110.   scvm_color_t* current_palette;
  111.   unsigned num_cycle;
  112.   scvm_cycle_t* cycle;
  113.   unsigned trans;
  114.   
  115.   // objects
  116.   unsigned num_object;
  117.   scvm_object_t** object;
  118.   
  119.   // scripts
  120.   scvm_script_t* entry;
  121.   scvm_script_t* exit;
  122.   unsigned num_script;
  123.   scvm_script_t** script;
  124.  
  125.   // boxes
  126.   unsigned num_box;
  127.   scc_box_t* box;
  128.   uint8_t* boxm;
  129. } scvm_room_t;
  130.  
  131. #define SCVM_ACTOR_IGNORE_BOXES        1
  132.  
  133. typedef struct scvm_actor {
  134.   unsigned id;
  135.   char* name;
  136.   scc_cost_dec_t costdec;
  137.   //scvm_room_t* room;
  138.   int room;
  139.   int x,y;
  140.   unsigned box;
  141.   unsigned width,height;
  142.   unsigned direction;
  143.   unsigned flags;
  144.   
  145.   // the anim step every anim_speed cycles
  146.   unsigned anim_speed, anim_cycle;
  147.   unsigned scale_x,scale_y;
  148.   unsigned layer;
  149.   
  150.   unsigned walk_speed_x,walk_speed_y;
  151.   unsigned walk_script;
  152.   unsigned elevation;
  153.  
  154.   unsigned init_frame;
  155.   unsigned walk_frame;
  156.   unsigned talk_start_frame, talk_end_frame;
  157.   unsigned stand_frame;
  158.   
  159.   
  160.   unsigned talk_color;
  161.   int talk_x,talk_y;
  162.   unsigned talk_script;
  163.  
  164.   unsigned walking;
  165.   int walk_dx, walk_dy, walk_err;
  166.   int walk_to_x, walk_to_y, walk_to_box;
  167.   int dstX, dstY, dst_box;
  168. } scvm_actor_t;
  169.  
  170. void scvm_actor_init(scvm_actor_t* a);
  171. void scvm_actor_set_default_frames(scvm_actor_t* a);
  172. void scvm_actor_put_at(scvm_actor_t* a, int x, int y, unsigned room);
  173. void scvm_actor_walk_to(scvm_actor_t* a, int x, int y);
  174. void scvm_actor_set_costume(scvm_actor_t* a, scc_cost_t* cost);
  175. void scvm_actor_animate(scvm_actor_t* a, int anim);
  176. void scvm_actor_step_anim(scvm_actor_t* a);
  177. int scvm_put_actor_at(scvm_t* vm, unsigned aid, int x, int y, unsigned rid);
  178. void scvm_put_actors(scvm_t* vm);
  179. void scvm_step_actors(scvm_t* vm);
  180.  
  181. #define SCVM_VIEW_SHAKE 1
  182. #define SCVM_VIEW_PAN   2
  183.  
  184. #define SCVM_VIEW_PALETTE_CHANGED (1<<16)
  185.  
  186. typedef struct scvm_view {
  187.   // room position on the screen
  188.   int room_start,room_end;
  189.   int scroll_left,scroll_right;
  190.   int camera_x, camera_dst_x;
  191.   int screen_width, screen_height;
  192.   int effect;
  193.   unsigned follow;
  194.   unsigned flags;
  195.   // palette used with the current room
  196.   scvm_palette_t palette;
  197. } scvm_view_t;
  198.  
  199. int scvm_view_draw(scvm_t* vm, scvm_view_t* view,
  200.                    uint8_t* buffer, int stride,
  201.                    unsigned width, unsigned height);
  202. void scvm_view_scale_palette(scvm_view_t* view,scvm_color_t* palette,
  203.                              unsigned red, unsigned green, unsigned blue,
  204.                              unsigned start, unsigned end);
  205. int scvm_abs_position_to_virtual(scvm_t* vm, int* dx, int* dy);
  206. void scvm_pan_camera_to(scvm_t* vm, int x);
  207. int scvm_set_camera_at(scvm_t* vm, int x);
  208. int scvm_move_camera(scvm_t* vm);
  209.  
  210. // variables used for communication between the scripts
  211. // and the VM.
  212. typedef struct scvm_vars {
  213.   // 000
  214.   int keypress;
  215.   int ego;
  216.   int camera_pos_x;
  217.   int have_msg;
  218.   int room;
  219.   int override;
  220.   int machine_speed;
  221.   int me;
  222.   int num_actor;
  223.   int sound_mode;
  224.   // 010
  225.   int current_drive;
  226.   int timer1;
  227.   int timer2;
  228.   int timer3;
  229.   int music_timer;
  230.   int actor_range_min;
  231.   int actor_range_max;
  232.   int camera_min_x;
  233.   int camera_max_x;
  234.   int timer_next;
  235.   // 020
  236.   int virtual_mouse_x;
  237.   int virtual_mouse_y;
  238.   int room_resource;
  239.   int last_sound;
  240.   int cutscene_exit_key;
  241.   int talk_actor;
  242.   int camera_fast_x;
  243.   int camera_script;
  244.   int pre_entry_script;
  245.   int post_entry_script;
  246.   // 030
  247.   int pre_exit_script;
  248.   int post_exit_script;
  249.   int verb_script;
  250.   int sentence_script;
  251.   int inventory_script;
  252.   int cutscene_start_script;
  253.   int cutscene_end_script;
  254.   int charinc;
  255.   int walk_to_object;
  256.   int debug_mode;
  257.   // 040
  258.   int heap_space;
  259.   int room_width;
  260.   int restart_key;
  261.   int pause_key;
  262.   int mouse_x;
  263.   int mouse_y;
  264.   int timer;
  265.   int timer4;
  266.   int soundcard;
  267.   int videomode;
  268.   // 050
  269.   int mainmenu_key;
  270.   int fixed_disk;
  271.   int cursor_state;
  272.   int userput;
  273.   int room_height;
  274.   int unknown1;
  275.   int sound_result;
  276.   int talk_stop_key;
  277.   int unknown2;
  278.   int fade_delay;
  279.   // 060
  280.   int no_subtitles;
  281.   int gui_entry_script;
  282.   int gui_exit_script;
  283.   int unknown3;
  284.   int sound_param[3];
  285.   int input_mode;
  286.   int memory_performance;
  287.   int video_performance;
  288.   // 070
  289.   int room_flag;
  290.   int game_loaded;
  291.   int new_room;
  292.   int unknown4;
  293.   int left_button_hold;
  294.   int right_button_hold;
  295.   int ems_space;
  296.   int unknown5[13];
  297.   // 090
  298.   int game_disk_msg;
  299.   int open_failed_msg;
  300.   int read_error_msg;
  301.   int pause_msg;
  302.   int restart_msg;
  303.   int quit_msg;
  304.   int save_button;
  305.   int load_button;
  306.   int play_button;
  307.   int cancel_button;
  308.   // 100
  309.   int quit_button;
  310.   int ok_button;
  311.   int save_disk_msg;
  312.   int enter_name_msg;
  313.   int not_saved_msg;
  314.   int not_loaded_msg;
  315.   int save_msg;
  316.   int load_msg;
  317.   int save_menu_title;
  318.   int load_menu_title;
  319.   // 110
  320.   int gui_colors;
  321.   int debug_password;
  322.   int unknown6[5];
  323.   int main_menu_title;
  324.   int random_num;
  325.   int timedate_year;
  326.   // 120
  327.   int unknown7[2];
  328.   int game_version;
  329.   int charset_mask;
  330.   int unknown8;
  331.   int timedate_hour;
  332.   int timedate_minute;
  333.   int unknown9;
  334.   int timedate_day;
  335.   int timedate_month;
  336.   // 130
  337.   //int padding[126];
  338. } PACKED_ATTRIB scvm_vars_t;
  339.  
  340.  
  341. typedef struct scvm_breakpoint {
  342.   unsigned id;
  343.   unsigned room_id;
  344.   unsigned script_id;
  345.   unsigned pos;
  346. } scvm_breakpoint_t;
  347.  
  348. typedef struct scvm_debug {
  349.   unsigned num_breakpoint;
  350.   scvm_breakpoint_t* breakpoint;
  351.  
  352.   int (*check_interrupt)(scvm_t* vm);
  353.  
  354. } scvm_debug_t;
  355.  
  356. struct scvm_backend_priv;
  357.  
  358. typedef struct scvm_backend {
  359.     // start the backend
  360.     int (*init)(struct scvm_backend* be);
  361.     void (*uninit)(struct scvm_backend* be);
  362.     // system callback
  363.     unsigned (*get_time)(struct scvm_backend_priv* be);
  364.     int (*random)(struct scvm_backend_priv* be,int min,int max);
  365.     void (*sleep)(struct scvm_backend_priv* be, unsigned time);
  366.     // rendering
  367.     int (*init_video)(struct scvm_backend_priv* be, unsigned width,
  368.                       unsigned height, unsigned bpp);
  369.     void (*update_palette)(struct scvm_backend_priv* be, scvm_color_t* pal);
  370.     void (*draw)(struct scvm_backend_priv* be, scvm_t* vm, scvm_view_t* view);
  371.     void (*flip)(struct scvm_backend_priv* be);
  372.     void (*uninit_video)(struct scvm_backend_priv* be);
  373.     // input
  374.     void (*check_events)(struct scvm_backend_priv* be, scvm_t* vm);
  375.  
  376.     struct scvm_backend_priv* priv;
  377. } scvm_backend_t;
  378.  
  379. // Don't change the first 4 as they match the
  380. // v6 resouces opcodes.
  381. #define SCVM_RES_SCRIPT  0
  382. #define SCVM_RES_SOUND   1
  383. #define SCVM_RES_COSTUME 2
  384. #define SCVM_RES_ROOM    3
  385. #define SCVM_RES_CHARSET 4
  386. #define SCVM_RES_OBJECT  5
  387. #define SCVM_RES_MAX     6
  388.  
  389. typedef int (*scvm_get_var_f)(struct scvm* vm,unsigned addr);
  390. typedef int (*scvm_set_var_f)(struct scvm* vm,unsigned addr, int val);
  391.  
  392. // VM states
  393.  
  394. #define SCVM_UNINITED         0
  395. #define SCVM_BOOT             5
  396.  
  397. #define SCVM_BEGIN_CYCLE      10
  398. #define SCVM_RUNNING          20
  399. #define SCVM_FINISHED_SCRIPTS 30
  400. #define SCVM_FINISH_CYCLE     40
  401.  
  402. #define SCVM_START_SCRIPT     100
  403.  
  404. #define SCVM_OPEN_ROOM        200
  405. #define SCVM_RUN_PRE_EXIT     210
  406. #define SCVM_RUN_EXCD         220
  407. #define SCVM_RUN_POST_EXIT    230
  408. #define SCVM_SETUP_ROOM       240
  409. #define SCVM_RUN_PRE_ENTRY    250
  410. #define SCVM_RUN_ENCD         260
  411. #define SCVM_RUN_POST_ENTRY   270
  412. #define SCVM_OPENED_ROOM      280
  413.  
  414. #define SCVM_CAMERA_FOLLOW_ACTOR                             300
  415. #define SCVM_CAMERA_START_FOLLOWING_ACTOR                    310
  416. #define SCVM_CAMERA_START_FOLLOWING_ACTOR_IN_ROOM            320
  417.  
  418. #define SCVM_RUN_INVENTORY                                   400
  419.  
  420. #define SCVM_CHECK_INPUT                                     480
  421. #define SCVM_CHECKED_INPUT                                   490
  422. #define SCVM_MOVE_CAMERA                                     500
  423. #define SCVM_WALK_ACTORS                                     510
  424.  
  425. // Input zone
  426. #define SCVM_INPUT_VERB   1
  427. #define SCVM_INPUT_ROOM   2
  428. #define SCVM_INPUT_KEY    4
  429.  
  430. struct scvm {
  431.   // Debuging data
  432.   scvm_debug_t* dbg;
  433.  
  434.   // variables
  435.   unsigned num_var;
  436.   int *var_mem;
  437.   scvm_vars_t* var;
  438.   scvm_get_var_f get_var[0x100];
  439.   scvm_set_var_f set_var[0x100];
  440.   // bit variables
  441.   unsigned num_bitvar;
  442.   uint8_t *bitvar;
  443.   // arrays
  444.   unsigned num_array;
  445.   scvm_array_t *array;
  446.   
  447.   // ressources
  448.   char *path;
  449.   char *basename;
  450.   unsigned num_file;
  451.   uint8_t file_key;
  452.   scc_fd_t** file;
  453.   scvm_res_type_t res[SCVM_RES_MAX];
  454.  
  455.   // object's persistant data
  456.   unsigned num_object, num_local_object;
  457.   scvm_object_pdata_t* object_pdata;
  458.   
  459.   // actors
  460.   unsigned num_actor;
  461.   scvm_actor_t* actor;
  462.   scvm_actor_t* current_actor;
  463.   
  464.   // current room
  465.   scvm_room_t* room;
  466.   unsigned next_room;
  467.   unsigned next_room_state;
  468.   // view
  469.   scvm_view_t* view;
  470.   
  471.   // threads
  472.   unsigned state;
  473.   unsigned num_thread;
  474.   unsigned cycle;
  475.   scvm_thread_t* current_thread;
  476.   scvm_thread_t* next_thread;
  477.   scvm_thread_t *thread;
  478.   scvm_op_t* optable;
  479.   scvm_op_t* suboptable;
  480.   unsigned time;
  481.  
  482.   // stack
  483.   unsigned stack_size;
  484.   unsigned stack_ptr;
  485.   int *stack;
  486.  
  487.   // input
  488.   int keypress, btnpress;
  489.   int key_state[256/8];
  490.   int btn_state;
  491.  
  492.   scvm_backend_t* backend;
  493. };
  494.  
  495. #define SCVM_ERR_SCRIPT_BOUND    -1
  496. #define SCVM_ERR_NO_OP           -2
  497. #define SCVM_ERR_STACK_OVERFLOW  -3
  498. #define SCVM_ERR_STACK_UNDERFLOW -4
  499. #define SCVM_ERR_BAD_ADDR        -5
  500. #define SCVM_ERR_ARRAY_BOUND     -6
  501. #define SCVM_ERR_ARRAY_TYPE      -7
  502. #define SCVM_ERR_JUMP_BOUND      -8
  503. #define SCVM_ERR_OUT_OF_ARRAY    -9
  504. #define SCVM_ERR_STRING_BOUND    -10
  505. #define SCVM_ERR_BAD_RESOURCE    -11
  506. #define SCVM_ERR_BAD_THREAD      -12
  507. #define SCVM_ERR_BAD_STATE       -13
  508. #define SCVM_ERR_BAD_ACTOR       -14
  509. #define SCVM_ERR_BAD_COSTUME     -15
  510. #define SCVM_ERR_OVERRIDE_OVERFLOW  -16
  511. #define SCVM_ERR_OVERRIDE_UNDERFLOW -17
  512. #define SCVM_ERR_BAD_OBJECT         -18
  513. #define SCVM_ERR_NO_ROOM            -19
  514. #define SCVM_ERR_BAD_PALETTE        -20
  515. #define SCVM_ERR_UNINITED_VM        -21
  516. #define SCVM_ERR_VIDEO_MODE         -22
  517. #define SCVM_ERR_INTERRUPTED        -23
  518. #define SCVM_ERR_BREAKPOINT         -24
  519.  
  520.  
  521. char* scvm_state_name(unsigned state);
  522.  
  523. int scvm_add_breakpoint(scvm_t* vm, unsigned room_id,
  524.                         unsigned script_id, unsigned pos);
  525.  
  526. int scvm_run_once(scvm_t* vm);
  527.  
  528. int scvm_run(scvm_t* vm);
  529.  
  530. unsigned scvm_get_time(scvm_t* vm);
  531.  
  532. int scvm_random(scvm_t* vm, int min, int max);
  533.  
  534. int scvm_init_video(scvm_t* vm, unsigned w, unsigned h, unsigned bpp);
  535.  
  536. void scvm_update_palette(scvm_t* vm, scvm_color_t* pal);
  537.  
  538. void scvm_draw(scvm_t* vm, scvm_view_t* view);
  539.  
  540. void scvm_sleep(scvm_t* vm, unsigned delay);
  541.  
  542. void scvm_flip(scvm_t* vm);
  543.  
  544. void scvm_uninit_video(scvm_t* vm);
  545.  
  546. int scvm_debugger(scvm_t* vm);
  547.