Subversion Repositories Kolibri OS

Rev

Rev 5235 | Rev 5239 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include "rsgame.h"
  2.  
  3. #include "rsgametext.h"
  4.  
  5. #include "rsgamemenu.h"
  6.  
  7. #include "rsgamedraw.h"
  8.  
  9. #include "rskos.h"
  10.  
  11. #include "rsgentex.h"
  12. #include "rssoundgen.h"
  13. #include "rsnoise.h"
  14.  
  15. #include "rs/rsplatform.h"
  16.  
  17.  
  18.  
  19.  
  20.  
  21. rs_game_t game;
  22.  
  23.  
  24. void texture_init(rs_texture_t *tex, int w, int h) {
  25.     tex->status = 1;
  26.     tex->w = w;
  27.     tex->h = h;
  28.     tex->data = malloc(w*h*4); // BGRA BGRA
  29. };
  30.  
  31. void texture_free(rs_texture_t *tex) {
  32.     free(tex->data);
  33.     tex->status = 0;
  34. };
  35.  
  36. void texture_clear(rs_texture_t *tex, unsigned int color) {
  37.     int i;
  38.     for (i = 0; i < tex->w * tex->h; i++) {
  39.         *((unsigned int*)(&tex->data[i*4])) = color;
  40.     };
  41. };
  42.  
  43. void texture_draw(rs_texture_t *dest, rs_texture_t *src, int x, int y, int mode) {
  44.    
  45.     int i; // y
  46.     int j; // x
  47.     int k; // color component
  48.    
  49.     int istart = (y < 0) ? -y : 0;
  50.     int iend = src->h - (( (y + src->h) > dest->h) ? (y + src->h - dest->h) : 0);
  51.    
  52.     int jstart = (x < 0) ? -x : 0;
  53.     int jend = src->w - (( (x + src->w) > dest->w) ? (x + src->w - dest->w) : 0);
  54.    
  55.     int ishift = 0;
  56.     int jshift = 0;
  57.    
  58.     float a; // alpha value
  59.    
  60.     if (mode & DRAW_TILED_FLAG) {
  61.         jshift = x;
  62.         ishift = y;
  63.         x = y = istart = jstart = 0;
  64.         iend = dest->h;
  65.         jend = dest->w;
  66.     };
  67.    
  68.     mode = mode & DRAW_MODE_MASK;
  69.    
  70.     int modvalue = (src->w*src->h*4);
  71.    
  72.     if (mode == DRAW_MODE_REPLACE) {
  73.         for (i = istart; i < iend; i++) {
  74.             for (j = jstart; j < jend; j++) {
  75.                 for (k = 0; k < 4; k++) {
  76.                     dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] = src->data[ (4*((i+ishift)*src->w + j + jshift) + k) % modvalue];
  77.                 };
  78.             };
  79.         };
  80.     }
  81.     else if (mode == DRAW_MODE_ADDITIVE) {
  82.         for (i = istart; i < iend; i++) {
  83.             for (j = jstart; j < jend; j++) {
  84.                 for (k = 0; k < 3; k++) { // Alpha channel is not added
  85.                     dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] =
  86.                         clamp_byte( dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] + src->data[ (4*((i+ishift)*src->w + j + jshift) + k) % modvalue] );
  87.                 };
  88.             };
  89.         };
  90.     }
  91.     else if (mode == DRAW_MODE_MULT) {
  92.         for (i = istart; i < iend; i++) {
  93.             for (j = jstart; j < jend; j++) {
  94.                 for (k = 0; k < 3; k++) { // Alpha channel is not added
  95.                     dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] =
  96.                         clamp_byte( dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] * src->data[ (4*((i+ishift)*src->w + j + jshift) + k) % modvalue] / 255 );
  97.                 };
  98.             };
  99.         };
  100.     }
  101.     else if (mode == DRAW_MODE_ALPHA) {
  102.         for (i = istart; i < iend; i++) {
  103.             for (j = jstart; j < jend; j++) {
  104.                 for (k = 0; k < 3; k++) {
  105.                     a = (1.0 * src->data[ (4*(i*src->w + j) + 3) % modvalue ] / 255.0);
  106.                     dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] =
  107.                          (unsigned char) ( (1.0-a) * dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ]
  108.                                           + a*src->data[ (4*((i+ishift)*src->w + j + jshift) + k) % modvalue] );
  109.                 };
  110.             };
  111.         };
  112.     };
  113.    
  114. };
  115.  
  116.  
  117.  
  118. void texture_set_pixel(rs_texture_t *tex, int x, int y, unsigned int color) {
  119.     *((unsigned int*) &tex->data[ 4 * ( (y)*tex->w + (x) ) + 0 ]) = color;
  120. };
  121.  
  122. void texture_draw_vline(rs_texture_t *tex, int x, int y, int l, unsigned int color) {
  123.     int i;
  124.     if (y+l >= tex->h) {
  125.         l = tex->h - y;
  126.     };
  127.     for (i = 0; i < l; i++) {
  128.         *((unsigned int*) &tex->data[ 4 * ( (y+i)*tex->w + (x) ) + 0 ]) = color;
  129.     };    
  130. };
  131.  
  132. void texture_draw_hline(rs_texture_t *tex, int x, int y, int l, unsigned int color) {
  133.     int i;
  134.     if (x+l >= tex->w) {
  135.         l = tex->w - x;
  136.     };
  137.     for (i = 0; i < l; i++) {
  138.         *((unsigned int*) &tex->data[ 4 * ( (y)*tex->w + (x+i) ) + 0 ]) = color;
  139.     };    
  140. };
  141.  
  142.  
  143.  
  144. void soundbuf_init(rs_soundbuf_t *snd, int length_samples) {
  145.     snd->status = 1;
  146.     snd->length_samples = length_samples;
  147.     snd->data = malloc(length_samples*2);
  148.     rskos_snd_create_buffer(&snd->hbuf, snd->data, length_samples);
  149. };
  150.  
  151. void soundbuf_free(rs_soundbuf_t *snd) {
  152.     snd->status = 0;
  153.     free(snd->data);
  154. };
  155.  
  156. void soundbuf_fill(rs_soundbuf_t *snd, int amp, int freq_div) {
  157.     int i;
  158.     for (i = 0; i < snd->length_samples; i++) {
  159.                 snd->data[i] = -amp/2 + amp/2*( ( (i % freq_div) > freq_div/2 ) ? 1 : 0 );
  160.         };
  161.         rskos_snd_update_buffer(&snd->hbuf, snd->data, snd->length_samples);
  162. };
  163.  
  164. void soundbuf_sin(rs_soundbuf_t *snd, float freq) {
  165.     int i;
  166.     int amp = 29000;
  167.     for (i = 0; i < snd->length_samples; i++) {
  168.                 snd->data[i] = ( 1.0 - 1.0*i/snd->length_samples ) * sin(freq*i) * amp;
  169.         };
  170.         rskos_snd_update_buffer(&snd->hbuf, snd->data, snd->length_samples);
  171. };
  172.  
  173. void soundbuf_sin_fade(rs_soundbuf_t *snd, float freq) {
  174.     int i;
  175.     int amp = 29000;
  176.     for (i = 0; i < snd->length_samples; i++) {
  177.                 snd->data[i] = ( 1.0 - 1.0*i/snd->length_samples ) * sin( ( (1.0 - 0.48*i/snd->length_samples) * freq ) *i) * amp;
  178.         };
  179.        
  180.        
  181.         /*
  182.        
  183.         // ok
  184.        
  185.         rs_sgen_init(2, snd->length_samples);
  186.         rs_sgen_func_pm(1, 880.0, 21.0, 0.3, 110.0, 0.3);
  187.         rs_sgen_func_normalize(1, 1.0);
  188.         rs_sgen_func_lowpass(0, 1, 1.0, 0.0, 1.0);
  189.         rs_sgen_wave_out(0);
  190.        
  191.         memcpy(snd->data, rs_sgen_reg.wave_out, snd->length_samples*2 );
  192.        
  193.         rs_sgen_term();
  194.        
  195.         */
  196.        
  197.         rskos_snd_update_buffer(&snd->hbuf, snd->data, snd->length_samples);
  198. };
  199.  
  200. void soundbuf_play(rs_soundbuf_t *snd) {
  201.     rskos_snd_play(&snd->hbuf, 0);
  202. };
  203.  
  204. void soundbuf_stop(rs_soundbuf_t *snd) {
  205.     rskos_snd_stop(&snd->hbuf);
  206. };
  207.  
  208.  
  209.  
  210. unsigned char clamp_byte(int value) {
  211.     value = value * (1 - (value >> 31)); // negative to zero
  212.     return (value > 255) ? 255 : value;
  213. };
  214.  
  215.  
  216. void game_reg_init() {
  217.    
  218.     game.loader_counter = 0;
  219.    
  220.     game.need_redraw = 1;
  221.    
  222.     game.score = 0;
  223.     game.time = 0;
  224.  
  225.     game.selected = 0;
  226.     game.selected_x = game.selected_y = 0;
  227.    
  228.     game.explosions_count = 0;
  229.    
  230.     game.tx = 0;
  231.     game.ty = 0;
  232.     game.tz = 0;
  233.  
  234. //    int i;
  235. //    for (i = 0; i < BULLETS_COUNT; i++) {
  236. //        game.bullet_x[i] = 0;
  237. //        game.bullet_y[i] = 0;
  238. //    };
  239. //    game.bullet_index = 0;
  240.    
  241.     game.status = STATUS_LOADING;
  242.  
  243.     game.window_scale = 1;
  244.    
  245. //    game.window_scale = 2;
  246. //    #ifndef RS_KOS
  247. //        game.window_scale = 3;
  248. //        window_scale_str[3] = '3';
  249. //    #endif
  250.    
  251.     game.keyboard_state = 0;
  252.    
  253.     game.menu_index = 0;
  254.     game.menu_item_index = 0;
  255. };
  256.  
  257.  
  258. int is_key_pressed(int mask) {
  259.     return IS_BIT_SET(game.keyboard_state, mask) ? 1 : 0;
  260. };
  261.  
  262. int seed = 0;
  263. int get_random_crystal() {
  264.     seed += 2;
  265.     return ( (seed + (get_time() & 0xFFFF) ) % CRYSTALS_COUNT) | CRYSTAL_VISIBLE_BIT;
  266. };
  267.  
  268. int game_check_and_explode() {
  269.     int x, y, i, item, match_count, found;
  270.     found = 0;
  271.    
  272.     // vertical lines
  273.     for (x = 0; x < FIELD_WIDTH; x++) {
  274.         match_count = 0;
  275.         item = 0xFF;
  276.         for (y = FIELD_HEIGHT-1; y >= 0; y--) {
  277.                
  278.             if ( IS_BIT_SET( FIELD_ITEM(x,y), CRYSTAL_MOVING_BIT ) || IS_BIT_CLEARED( FIELD_ITEM(x,y), CRYSTAL_VISIBLE_BIT) ) {
  279.                 item = 0xFF;
  280.                 match_count = 0;
  281.                 continue;
  282.             };
  283.            
  284.             if ( (FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK) == item) {
  285.                 match_count++;
  286.             }
  287.             else {
  288.                 if (match_count >= 2) {
  289.                     found = 1;
  290.                     for (i = y+1; i < y+1+match_count+1; i++) {
  291.                         BIT_SET( FIELD_ITEM(x, i), CRYSTAL_EXPLODED_BIT );
  292.                     };
  293.                 }
  294.                 item = FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK;
  295.                 match_count = 0;
  296.             };
  297.         };
  298.         if (match_count >= 2) { // last
  299.             found = 1;
  300.             for (i = y+1; i < y+1+match_count+1; i++) {
  301.                 BIT_SET( FIELD_ITEM(x, i), CRYSTAL_EXPLODED_BIT );
  302.             };
  303.         };
  304.     };
  305.    
  306.     // horizontal lines
  307.     for (y = 0; y < FIELD_HEIGHT; y++) {
  308.            
  309.         match_count = 0;
  310.         item = 0xFF;
  311.         for (x = FIELD_WIDTH-1; x >= 0; x--) {
  312.  
  313.             if ( IS_BIT_SET( FIELD_ITEM(x,y), CRYSTAL_MOVING_BIT ) || IS_BIT_CLEARED( FIELD_ITEM(x,y), CRYSTAL_VISIBLE_BIT) ) {
  314.                 item = 0xFF;
  315.                 match_count = 0;
  316.                 continue;
  317.             };
  318.  
  319.             if ( (FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK) == item) {
  320.                 match_count++;
  321.             }
  322.             else {
  323.                 if (match_count >= 2) {
  324.                     found = 1;
  325.                     for (i = x+1; i < x+1+match_count+1; i++) {
  326.                         BIT_SET( FIELD_ITEM(i, y), CRYSTAL_EXPLODED_BIT );
  327.                     };
  328.                 }
  329.                 item = FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK;
  330.                 match_count = 0;
  331.             };
  332.         };
  333.         if (match_count >= 2) { // last
  334.             found = 1;
  335.             for (i = x+1; i < x+1+match_count+1; i++) {
  336.                 BIT_SET( FIELD_ITEM(i, y), CRYSTAL_EXPLODED_BIT );
  337.             };
  338.         };
  339.     };
  340.  
  341.     for (i = 0; i < FIELD_LENGTH; i++) {
  342.         if (IS_BIT_SET(game.field[i], CRYSTAL_EXPLODED_BIT)) {
  343.             game.field[i] = 0;
  344.             game.score++;
  345.             if (game.explosions_count < EXPLOSIONS_MAX_COUNT-1) {
  346.                 game.explosions[game.explosions_count] = ( i % FIELD_WIDTH ) | ( (i / FIELD_WIDTH) << 8);
  347.                 game.explosions_count++;
  348.             };
  349.         };
  350.     };
  351.    
  352.     if (game.score > 99) {
  353.         game.status = STATUS_MENU;
  354.         game.need_redraw = 1;
  355.     };
  356.    
  357.     if (found) {
  358.         game.need_redraw = 1;
  359.     };
  360.    
  361.     return found;
  362. };
  363.  
  364. void game_fall() {
  365.     int x, y, fall;
  366.     for (x = 0; x < FIELD_WIDTH; x++) {
  367.         fall = 0;
  368.         for (y = FIELD_HEIGHT-1; y > 0; y--) {
  369.             fall |= !(FIELD_ITEM(x, y) & CRYSTAL_VISIBLE_BIT);
  370.             if (fall) {
  371.                 FIELD_ITEM(x, y) = FIELD_ITEM(x, y-1) | CRYSTAL_MOVING_BIT;
  372.                 game.need_redraw = 1;
  373.             }
  374.             else {
  375.                 BIT_CLEAR( FIELD_ITEM(x, y), CRYSTAL_MOVING_BIT );
  376.             };
  377.         };
  378.         if ( (fall) || ( IS_BIT_CLEARED(FIELD_ITEM(x,0), CRYSTAL_VISIBLE_BIT) ) ) {
  379.             FIELD_ITEM(x, 0) = get_random_crystal();
  380.         };
  381.     };
  382. };
  383.  
  384. int process_timer = 0;
  385.  
  386. void GameProcess() {
  387.    
  388.     if (game.status == STATUS_LOADING) {
  389.         game.loader_counter++;
  390.         if (game.loader_counter == 2) {
  391.                
  392.             game_textures_init_stage2();
  393.                
  394.            
  395.        
  396.             game.status = STATUS_MENU;
  397.             game.need_redraw = 1;
  398.  
  399.         };
  400.     }
  401.     else if (game.status == STATUS_PLAYING) {
  402.            
  403.            
  404.         process_timer++;
  405.  
  406.         if (process_timer > 3) {
  407.             game_check_and_explode();
  408.             game_fall();
  409.             process_timer = 0;
  410.         };
  411.        
  412.         int i;
  413.         for (i = 0; i < game.explosions_count; i++) {
  414.             game.need_redraw = 1;
  415.             game.explosions[i] = (game.explosions[i] & 0xFFFF) | ( ((game.explosions[i]>>16)+1) << 16 );
  416.             if ( (game.explosions[i] >> 16) >= EXPLOSION_FRAMES_COUNT ) {
  417.                 game.explosions[i] = game.explosions[game.explosions_count-1];
  418.                 game.explosions_count--;
  419.                 i--;
  420.             };
  421.         };
  422.        
  423.         if ((game.time+1)/25 != game.time/25) {
  424.             game.need_redraw = 1;
  425.         };
  426.        
  427.         game.time++;
  428.        
  429.        
  430.        
  431.  
  432.     };
  433.  
  434.     game_draw();
  435.  
  436. }
  437.  
  438.  
  439.  
  440.  
  441.  
  442. void GameInit() {
  443.  
  444.     game_reg_init();
  445.    
  446.     game.field = malloc( FIELD_LENGTH );
  447.     int i;
  448.     for (i = 0; i < FIELD_LENGTH; i++) {
  449.         game.field[i] = (unsigned char) (0.99 * fabs(rs_noise(i, 10)) * CRYSTALS_COUNT) | CRYSTAL_VISIBLE_BIT;
  450.     };
  451. //    memset( game.field, 0, FIELD_LENGTH );
  452.    
  453.     game.scaled_framebuffer = malloc(GAME_WIDTH*game.window_scale * GAME_HEIGHT*game.window_scale * 3);
  454.     DEBUG10f("scaled framebuffer: %d (window_scale = %d) \n", game.window_scale * GAME_WIDTH * GAME_HEIGHT * 3, game.window_scale);
  455.    
  456.     game_font_init();
  457.    
  458.     game_textures_init_stage1();
  459.    
  460.    
  461.  
  462.     #ifndef RS_KOS
  463.         rs_audio_init(RS_AUDIO_FMT_MONO16, RS_AUDIO_FREQ_16000, 0);
  464.     #endif
  465.  
  466.     soundbuf_init(&game.sound_test1, 2048);
  467. //    soundbuf_fill(&game.sound_test1, 2, 50);
  468.     soundbuf_sin_fade(&game.sound_test1, 0.7);
  469.  
  470.     soundbuf_init(&game.sound_test2, 1024);
  471.     //soundbuf_fill(&game.sound_test2, 8, 40);
  472.     soundbuf_sin(&game.sound_test2, 0.48);
  473.  
  474.     soundbuf_init(&game.sound_test3, 1024);
  475.     //soundbuf_fill(&game.sound_test3, 12, 60);
  476.     soundbuf_sin(&game.sound_test3, 0.24);
  477.  
  478.  
  479. };
  480.  
  481.  
  482. void GameTerm() {
  483.  
  484.  
  485.     DEBUG10("--- Game Term ---");
  486.    
  487.     free(game.field);
  488.  
  489.     #ifndef RS_KOS
  490.         rs_audio_term();
  491.     #endif
  492.  
  493.     game_font_term();
  494.    
  495.     game_textures_free();
  496.    
  497.    
  498.    
  499.  
  500.    
  501.     soundbuf_free(&game.sound_test1);
  502.     soundbuf_free(&game.sound_test2);
  503.     soundbuf_free(&game.sound_test3);
  504.    
  505.  
  506. };
  507.  
  508. // ------------ #Event Functions ------------
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515. void GameKeyDown(int key, int first) {
  516.    
  517.     if (key == RS_KEY_A) {
  518.    
  519.         game.need_redraw = 1;
  520.        
  521.     };    
  522.    
  523.     switch (key) {
  524.         case RS_KEY_LEFT:
  525.             BIT_SET(game.keyboard_state, RS_ARROW_LEFT_MASK);
  526.             break;
  527.         case RS_KEY_RIGHT:
  528.             BIT_SET(game.keyboard_state, RS_ARROW_RIGHT_MASK);
  529.             break;
  530.         case RS_KEY_UP:
  531.             BIT_SET(game.keyboard_state, RS_ARROW_UP_MASK);
  532.             break;
  533.         case RS_KEY_DOWN:
  534.             BIT_SET(game.keyboard_state, RS_ARROW_DOWN_MASK);
  535.             break;
  536.         case RS_KEY_A:
  537.             BIT_SET(game.keyboard_state, RS_ATTACK_KEY_MASK);
  538. //            game.shoot_keypressed = 1;
  539.             break;
  540.     };
  541.    
  542.    
  543.     if (game.status == STATUS_MENU) {
  544.    
  545.         switch (key) {
  546.             case RS_KEY_LEFT:
  547.                 BIT_SET(game.keyboard_state, RS_ARROW_LEFT_MASK);
  548.                 //PlayBuffer(hBuff, 0);
  549.                 break;
  550.             case RS_KEY_RIGHT:
  551.                 BIT_SET(game.keyboard_state, RS_ARROW_RIGHT_MASK);
  552.                 //StopBuffer(hBuff);
  553.                 break;
  554.             case RS_KEY_UP:
  555.                 BIT_SET(game.keyboard_state, RS_ARROW_UP_MASK);
  556.                 menu_cursor_up();
  557.                 //ResetBuffer(hBuff, 0);
  558.                 break;
  559.             case RS_KEY_DOWN:
  560.                 BIT_SET(game.keyboard_state, RS_ARROW_DOWN_MASK);
  561.                 menu_cursor_down();
  562.                 break;
  563.             case RS_KEY_RETURN:
  564.                 menu_cursor_click();
  565.                 break;
  566.             case RS_KEY_ESCAPE:
  567.                 menu_open(0);
  568.                 break;
  569.         };
  570.  
  571.     };
  572.    
  573.     if (game.status == STATUS_PLAYING) {
  574.        
  575.  
  576.        
  577.         if (key == RS_KEY_SPACE) {
  578.            
  579.             game.score = 101;
  580.            
  581.            
  582.         };
  583.        
  584.     };
  585.  
  586. };
  587.  
  588. void GameKeyUp(int key) {
  589.  
  590.     switch (key) {
  591.         case RS_KEY_LEFT:
  592.             BIT_CLEAR(game.keyboard_state, RS_ARROW_LEFT_MASK);
  593.             break;
  594.         case RS_KEY_RIGHT:
  595.             BIT_CLEAR(game.keyboard_state, RS_ARROW_RIGHT_MASK);
  596.             break;
  597.         case RS_KEY_UP:
  598.             BIT_CLEAR(game.keyboard_state, RS_ARROW_UP_MASK);
  599.             break;
  600.         case RS_KEY_DOWN:
  601.             BIT_CLEAR(game.keyboard_state, RS_ARROW_DOWN_MASK);
  602.             break;
  603.         case RS_KEY_A:
  604.             BIT_CLEAR(game.keyboard_state, RS_ATTACK_KEY_MASK);
  605.             break;
  606.     };
  607.  
  608. };
  609.  
  610. void GameMouseDown(int x, int y) {
  611.    
  612.     game.need_redraw = 1;
  613.    
  614.     game.tx = x;
  615.     game.ty = y;
  616.    
  617.     if (game.status == STATUS_MENU) {
  618.         game.time = 0;
  619.         game.score = 0;
  620.         game.status = STATUS_PLAYING;
  621.         return;
  622.     };
  623.    
  624.     if (game.status == STATUS_PLAYING) {
  625.        
  626.         unsigned int field_x = (unsigned int) (x - FIELD_X0) / CRYSTAL_SIZE;
  627.         if (field_x != rs_clamp_i(field_x, 0, FIELD_WIDTH-1)) {
  628.             return;
  629.         };
  630.        
  631.         unsigned int field_y = (unsigned int) (y - FIELD_Y0) / CRYSTAL_SIZE;
  632.         if (field_y != rs_clamp_i(field_y, 0, FIELD_HEIGHT-1)) {
  633.             return;
  634.         };
  635.        
  636.         //FIELD_ITEM(field_x, field_y) = 0;
  637.        
  638.         if (!game.selected) {
  639.             game.selected = 1;
  640.             game.selected_x = field_x;
  641.             game.selected_y = field_y;                                                                      
  642.            
  643.         }
  644.         else {
  645.            
  646.             if ( abs(game.selected_x - field_x) + abs(game.selected_y - field_y) == 1 ) {
  647.                 game.selected = 0;
  648.                
  649.                 // Trying to swap
  650.                 int temp_crystal = FIELD_ITEM(field_x, field_y);
  651.                 FIELD_ITEM(field_x, field_y) = FIELD_ITEM(game.selected_x, game.selected_y);
  652.                 FIELD_ITEM(game.selected_x, game.selected_y) = temp_crystal;
  653.                
  654.                 if ( !game_check_and_explode() ) {
  655.                     FIELD_ITEM(game.selected_x, game.selected_y) = FIELD_ITEM(field_x, field_y);
  656.                     FIELD_ITEM(field_x, field_y) = temp_crystal;
  657.                 }
  658.                 else {
  659.                     // success
  660.                     process_timer = 0;
  661.                 };
  662.                
  663.             }
  664.             else {
  665.                 if ( (game.selected_x != field_x) && (game.selected_y != field_y) ) {
  666.                     game.selected_x = field_x;
  667.                     game.selected_y = field_y;
  668.                 }
  669.                 else {
  670.                     game.selected = 0;
  671.                 };
  672.             };
  673.            
  674.         };
  675.        
  676. //        int i;
  677. //        for (i = field_y; i > 0; i--) {
  678. //            FIELD_ITEM(field_x, i) = FIELD_ITEM(field_x, (i-1) );
  679. //        };
  680. //        FIELD_ITEM(field_x, 0) = 0;
  681.        
  682.     };
  683.    
  684. };
  685.  
  686. void GameMouseUp(int x, int y) {
  687.     //
  688. };
  689.  
  690.  
  691. void game_ding(int i) {
  692.    
  693.     switch (i) {
  694.         case 0:
  695.             soundbuf_play(&game.sound_test2);
  696.             break;
  697.         case 1:
  698.             soundbuf_play(&game.sound_test3);
  699.             break;
  700.     };
  701.    
  702. };
  703.