Subversion Repositories Kolibri OS

Rev

Rev 5237 | Rev 5243 | 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.process_timer = 0;
  221.    
  222.     game.need_redraw = 1;
  223.    
  224.     game.score = 0;
  225.     game.time = 0;
  226.  
  227.     game.selected = 0;
  228.     game.selected_x = game.selected_y = 0;
  229.    
  230.     game.explosions_count = 0;
  231.    
  232.     game.tx = 0;
  233.     game.ty = 0;
  234.     game.tz = 0;
  235.  
  236. //    int i;
  237. //    for (i = 0; i < BULLETS_COUNT; i++) {
  238. //        game.bullet_x[i] = 0;
  239. //        game.bullet_y[i] = 0;
  240. //    };
  241. //    game.bullet_index = 0;
  242.    
  243.     game.status = STATUS_LOADING;
  244.  
  245.     game.window_scale = 1;
  246.    
  247. //    game.window_scale = 2;
  248. //    #ifndef RS_KOS
  249. //        game.window_scale = 3;
  250. //        window_scale_str[3] = '3';
  251. //    #endif
  252.    
  253.     game.keyboard_state = 0;
  254.    
  255.     game.menu_index = 0;
  256.     game.menu_item_index = 0;
  257. };
  258.  
  259.  
  260. int is_key_pressed(int mask) {
  261.     return IS_BIT_SET(game.keyboard_state, mask) ? 1 : 0;
  262. };
  263.  
  264. int seed = 0;
  265. int get_random_crystal() {
  266.     seed += 2;
  267.     return ( (seed + (get_time() & 0xFFFF) ) % CRYSTALS_COUNT) | CRYSTAL_VISIBLE_BIT;
  268. };
  269.  
  270. int game_check_and_explode() {
  271.     int x, y, i, item, match_count, found;
  272.     found = 0;
  273.    
  274.     // vertical lines
  275.     for (x = 0; x < FIELD_WIDTH; x++) {
  276.         match_count = 0;
  277.         item = 0xFF;
  278.         for (y = FIELD_HEIGHT-1; y >= 0; y--) {
  279.                
  280.             if ( IS_BIT_SET( FIELD_ITEM(x,y), CRYSTAL_MOVING_BIT ) || IS_BIT_CLEARED( FIELD_ITEM(x,y), CRYSTAL_VISIBLE_BIT) ) {
  281.                 item = 0xFF;
  282.                 match_count = 0;
  283.                 continue;
  284.             };
  285.            
  286.             if ( (FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK) == item) {
  287.                 match_count++;
  288.             }
  289.             else {
  290.                 if (match_count >= 2) {
  291.                     found = 1;
  292.                     for (i = y+1; i < y+1+match_count+1; i++) {
  293.                         BIT_SET( FIELD_ITEM(x, i), CRYSTAL_EXPLODED_BIT );
  294.                     };
  295.                 }
  296.                 item = FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK;
  297.                 match_count = 0;
  298.             };
  299.         };
  300.         if (match_count >= 2) { // last
  301.             found = 1;
  302.             for (i = y+1; i < y+1+match_count+1; i++) {
  303.                 BIT_SET( FIELD_ITEM(x, i), CRYSTAL_EXPLODED_BIT );
  304.             };
  305.         };
  306.     };
  307.    
  308.     // horizontal lines
  309.     for (y = 0; y < FIELD_HEIGHT; y++) {
  310.            
  311.         match_count = 0;
  312.         item = 0xFF;
  313.         for (x = FIELD_WIDTH-1; x >= 0; x--) {
  314.  
  315.             if ( IS_BIT_SET( FIELD_ITEM(x,y), CRYSTAL_MOVING_BIT ) || IS_BIT_CLEARED( FIELD_ITEM(x,y), CRYSTAL_VISIBLE_BIT) ) {
  316.                 item = 0xFF;
  317.                 match_count = 0;
  318.                 continue;
  319.             };
  320.  
  321.             if ( (FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK) == item) {
  322.                 match_count++;
  323.             }
  324.             else {
  325.                 if (match_count >= 2) {
  326.                     found = 1;
  327.                     for (i = x+1; i < x+1+match_count+1; i++) {
  328.                         BIT_SET( FIELD_ITEM(i, y), CRYSTAL_EXPLODED_BIT );
  329.                     };
  330.                 }
  331.                 item = FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK;
  332.                 match_count = 0;
  333.             };
  334.         };
  335.         if (match_count >= 2) { // last
  336.             found = 1;
  337.             for (i = x+1; i < x+1+match_count+1; i++) {
  338.                 BIT_SET( FIELD_ITEM(i, y), CRYSTAL_EXPLODED_BIT );
  339.             };
  340.         };
  341.     };
  342.  
  343.     for (i = 0; i < FIELD_LENGTH; i++) {
  344.         if (IS_BIT_SET(game.field[i], CRYSTAL_EXPLODED_BIT)) {
  345.             game.field[i] = 0;
  346.             game.score++;
  347.             if (game.explosions_count < EXPLOSIONS_MAX_COUNT-1) {
  348.                 game.explosions[game.explosions_count] = ( i % FIELD_WIDTH ) | ( (i / FIELD_WIDTH) << 8);
  349.                 game.explosions_count++;
  350.             };
  351.         };
  352.     };
  353.    
  354.     if (game.score > 99) {
  355.         game.status = STATUS_MENU;
  356.         game.need_redraw = 1;
  357.     };
  358.    
  359.     if (found) {
  360.         game.need_redraw = 1;
  361.     };
  362.    
  363.     return found;
  364. };
  365.  
  366. void game_fall() {
  367.     int x, y, fall;
  368.     for (x = 0; x < FIELD_WIDTH; x++) {
  369.         fall = 0;
  370.         for (y = FIELD_HEIGHT-1; y > 0; y--) {
  371.             fall |= !(FIELD_ITEM(x, y) & CRYSTAL_VISIBLE_BIT);
  372.             if (fall) {
  373.                 FIELD_ITEM(x, y) = FIELD_ITEM(x, y-1) | CRYSTAL_MOVING_BIT;
  374.                 game.need_redraw = 1;
  375.             }
  376.             else {
  377.                 BIT_CLEAR( FIELD_ITEM(x, y), CRYSTAL_MOVING_BIT );
  378.             };
  379.         };
  380.         if ( (fall) || ( IS_BIT_CLEARED(FIELD_ITEM(x,0), CRYSTAL_VISIBLE_BIT) ) ) {
  381.             FIELD_ITEM(x, 0) = get_random_crystal();
  382.         };
  383.     };
  384. };
  385.  
  386.  
  387.  
  388. void GameProcess() {
  389.    
  390.     if (game.status == STATUS_LOADING) {
  391.         game.loader_counter++;
  392.         if (game.loader_counter == 2) {
  393.                
  394.             game_textures_init_stage2();
  395.                
  396.            
  397.        
  398.             game.status = STATUS_MENU;
  399.             game.need_redraw = 1;
  400.  
  401.         };
  402.     }
  403.     else if (game.status == STATUS_PLAYING) {
  404.            
  405.         game.process_timer++;
  406.  
  407.         if (game.process_timer > ANIMATION_PROCESS_TIMER_LIMIT) {
  408.             game_check_and_explode();
  409.             game_fall();
  410.             game.process_timer = 0;
  411.         };
  412.        
  413.         int i;
  414.         for (i = 0; i < game.explosions_count; i++) {
  415.             game.need_redraw = 1;
  416.             game.explosions[i] = (game.explosions[i] & 0xFFFF) | ( ((game.explosions[i]>>16)+1) << 16 );
  417.             if ( (game.explosions[i] >> 16) >= EXPLOSION_FRAMES_COUNT ) {
  418.                 game.explosions[i] = game.explosions[game.explosions_count-1];
  419.                 game.explosions_count--;
  420.                 i--;
  421.             };
  422.         };
  423.        
  424.         if ((game.time+1)/25 != game.time/25) {
  425.             game.need_redraw = 1;
  426.         };
  427.        
  428.         game.time++;
  429.        
  430.        
  431.        
  432.  
  433.     };
  434.  
  435.     game_draw();
  436.  
  437. }
  438.  
  439.  
  440.  
  441.  
  442.  
  443. void GameInit() {
  444.  
  445.     game_reg_init();
  446.    
  447.     game.field = malloc( FIELD_LENGTH );
  448.     int i;
  449.     for (i = 0; i < FIELD_LENGTH; i++) {
  450.         game.field[i] = (unsigned char) (0.99 * fabs(rs_noise(i, 10)) * CRYSTALS_COUNT) | CRYSTAL_VISIBLE_BIT;
  451.     };
  452. //    memset( game.field, 0, FIELD_LENGTH );
  453.    
  454.     game.scaled_framebuffer = malloc(GAME_WIDTH*game.window_scale * GAME_HEIGHT*game.window_scale * 3);
  455.     DEBUG10f("scaled framebuffer: %d (window_scale = %d) \n", game.window_scale * GAME_WIDTH * GAME_HEIGHT * 3, game.window_scale);
  456.    
  457.     game_font_init();
  458.    
  459.     game_textures_init_stage1();
  460.    
  461.    
  462.  
  463.     #ifndef RS_KOS
  464.         rs_audio_init(RS_AUDIO_FMT_MONO16, RS_AUDIO_FREQ_16000, 0);
  465.     #endif
  466.  
  467.     soundbuf_init(&game.sound_test1, 2048);
  468. //    soundbuf_fill(&game.sound_test1, 2, 50);
  469.     soundbuf_sin_fade(&game.sound_test1, 0.7);
  470.  
  471.     soundbuf_init(&game.sound_test2, 1024);
  472.     //soundbuf_fill(&game.sound_test2, 8, 40);
  473.     soundbuf_sin(&game.sound_test2, 0.48);
  474.  
  475.     soundbuf_init(&game.sound_test3, 1024);
  476.     //soundbuf_fill(&game.sound_test3, 12, 60);
  477.     soundbuf_sin(&game.sound_test3, 0.24);
  478.  
  479.  
  480. };
  481.  
  482.  
  483. void GameTerm() {
  484.  
  485.  
  486.     DEBUG10("--- Game Term ---");
  487.    
  488.     free(game.field);
  489.  
  490.     #ifndef RS_KOS
  491.         rs_audio_term();
  492.     #endif
  493.  
  494.     game_font_term();
  495.    
  496.     game_textures_free();
  497.    
  498.    
  499.    
  500.  
  501.    
  502.     soundbuf_free(&game.sound_test1);
  503.     soundbuf_free(&game.sound_test2);
  504.     soundbuf_free(&game.sound_test3);
  505.    
  506.  
  507. };
  508.  
  509. // ------------ #Event Functions ------------
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516. void GameKeyDown(int key, int first) {
  517.    
  518.     if (key == RS_KEY_A) {
  519.    
  520.         game.need_redraw = 1;
  521.        
  522.     };    
  523.    
  524.     switch (key) {
  525.         case RS_KEY_LEFT:
  526.             BIT_SET(game.keyboard_state, RS_ARROW_LEFT_MASK);
  527.             break;
  528.         case RS_KEY_RIGHT:
  529.             BIT_SET(game.keyboard_state, RS_ARROW_RIGHT_MASK);
  530.             break;
  531.         case RS_KEY_UP:
  532.             BIT_SET(game.keyboard_state, RS_ARROW_UP_MASK);
  533.             break;
  534.         case RS_KEY_DOWN:
  535.             BIT_SET(game.keyboard_state, RS_ARROW_DOWN_MASK);
  536.             break;
  537.         case RS_KEY_A:
  538.             BIT_SET(game.keyboard_state, RS_ATTACK_KEY_MASK);
  539. //            game.shoot_keypressed = 1;
  540.             break;
  541.     };
  542.    
  543.    
  544.     if (game.status == STATUS_MENU) {
  545.    
  546.         switch (key) {
  547.             case RS_KEY_LEFT:
  548.                 BIT_SET(game.keyboard_state, RS_ARROW_LEFT_MASK);
  549.                 //PlayBuffer(hBuff, 0);
  550.                 break;
  551.             case RS_KEY_RIGHT:
  552.                 BIT_SET(game.keyboard_state, RS_ARROW_RIGHT_MASK);
  553.                 //StopBuffer(hBuff);
  554.                 break;
  555.             case RS_KEY_UP:
  556.                 BIT_SET(game.keyboard_state, RS_ARROW_UP_MASK);
  557.                 menu_cursor_up();
  558.                 //ResetBuffer(hBuff, 0);
  559.                 break;
  560.             case RS_KEY_DOWN:
  561.                 BIT_SET(game.keyboard_state, RS_ARROW_DOWN_MASK);
  562.                 menu_cursor_down();
  563.                 break;
  564.             case RS_KEY_RETURN:
  565.                 menu_cursor_click();
  566.                 break;
  567.             case RS_KEY_ESCAPE:
  568.                 menu_open(0);
  569.                 break;
  570.         };
  571.  
  572.     };
  573.    
  574.     if (game.status == STATUS_PLAYING) {
  575.        
  576.  
  577.        
  578.         if (key == RS_KEY_SPACE) {
  579.            
  580.             game.score = 101;
  581.            
  582.            
  583.         };
  584.        
  585.     };
  586.  
  587. };
  588.  
  589. void GameKeyUp(int key) {
  590.  
  591.     switch (key) {
  592.         case RS_KEY_LEFT:
  593.             BIT_CLEAR(game.keyboard_state, RS_ARROW_LEFT_MASK);
  594.             break;
  595.         case RS_KEY_RIGHT:
  596.             BIT_CLEAR(game.keyboard_state, RS_ARROW_RIGHT_MASK);
  597.             break;
  598.         case RS_KEY_UP:
  599.             BIT_CLEAR(game.keyboard_state, RS_ARROW_UP_MASK);
  600.             break;
  601.         case RS_KEY_DOWN:
  602.             BIT_CLEAR(game.keyboard_state, RS_ARROW_DOWN_MASK);
  603.             break;
  604.         case RS_KEY_A:
  605.             BIT_CLEAR(game.keyboard_state, RS_ATTACK_KEY_MASK);
  606.             break;
  607.     };
  608.  
  609. };
  610.  
  611. typedef struct {
  612.     int a;
  613.     int b;
  614.     unsigned short c;
  615.     unsigned short d;
  616. } cc_t;
  617.  
  618. void GameMouseDown(int x, int y) {
  619.    
  620.    
  621.     game.need_redraw = 1;
  622.    
  623.     game.tx = x;
  624.     game.ty = y;
  625.    
  626.     if (game.status == STATUS_MENU) {
  627.         game.time = 0;
  628.         game.score = 0;
  629.         game.status = STATUS_PLAYING;
  630.         return;
  631.     };
  632.    
  633.     if (game.status == STATUS_PLAYING) {
  634.        
  635.         unsigned int field_x = (unsigned int) (x - FIELD_X0) / CRYSTAL_SIZE;
  636.         if (field_x != rs_clamp_i(field_x, 0, FIELD_WIDTH-1)) {
  637.             return;
  638.         };
  639.        
  640.         unsigned int field_y = (unsigned int) (y - FIELD_Y0) / CRYSTAL_SIZE;
  641.         if (field_y != rs_clamp_i(field_y, 0, FIELD_HEIGHT-1)) {
  642.             return;
  643.         };
  644.        
  645.         //FIELD_ITEM(field_x, field_y) = 0;
  646.        
  647.         if (!game.selected) {
  648.             game.selected = 1;
  649.             game.selected_x = field_x;
  650.             game.selected_y = field_y;                                                                      
  651.            
  652.         }
  653.         else {
  654.            
  655.             if ( abs(game.selected_x - field_x) + abs(game.selected_y - field_y) == 1 ) {
  656.                 game.selected = 0;
  657.                
  658.                 // Trying to swap
  659.                 int temp_crystal = FIELD_ITEM(field_x, field_y);
  660.                 FIELD_ITEM(field_x, field_y) = FIELD_ITEM(game.selected_x, game.selected_y);
  661.                 FIELD_ITEM(game.selected_x, game.selected_y) = temp_crystal;
  662.                
  663.                 if ( !game_check_and_explode() ) {
  664.                     FIELD_ITEM(game.selected_x, game.selected_y) = FIELD_ITEM(field_x, field_y);
  665.                     FIELD_ITEM(field_x, field_y) = temp_crystal;
  666.                 }
  667.                 else {
  668.                     // success
  669.                     game.process_timer = 0;
  670.                 };
  671.                
  672.             }
  673.             else {
  674.                 if ( (game.selected_x != field_x) && (game.selected_y != field_y) ) {
  675.                     game.selected_x = field_x;
  676.                     game.selected_y = field_y;
  677.                 }
  678.                 else {
  679.                     game.selected = 0;
  680.                 };
  681.             };
  682.            
  683.         };
  684.        
  685. //        int i;
  686. //        for (i = field_y; i > 0; i--) {
  687. //            FIELD_ITEM(field_x, i) = FIELD_ITEM(field_x, (i-1) );
  688. //        };
  689. //        FIELD_ITEM(field_x, 0) = 0;
  690.        
  691.     };
  692.    
  693. };
  694.  
  695. void GameMouseUp(int x, int y) {
  696.     //
  697. };
  698.  
  699.  
  700. void game_ding(int i) {
  701.    
  702.     switch (i) {
  703.         case 0:
  704.             soundbuf_play(&game.sound_test2);
  705.             break;
  706.         case 1:
  707.             soundbuf_play(&game.sound_test3);
  708.             break;
  709.     };
  710.    
  711. };
  712.