Advertisement
Lauda

Untitled

Jun 7th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.45 KB | None | 0 0
  1. #include "podesavanja.h"
  2. #include <stdio.h>
  3. #include <curses.h>
  4. #include <string.h>
  5. #include "utils.h"
  6.  
  7. // Default podesavanja
  8. const podesavanja default_vrijednosti = {1 /* Debug */, 10 /* Maksimalni rezultat */};
  9.  
  10. void ucitaj_podesavanja(podesavanja *conf)
  11. {
  12.     FILE *fp=fopen(CONF_FILE, "rb");
  13.     if(fp != NULL)
  14.     {
  15.         fread(conf, sizeof(podesavanja), 1, fp);
  16.  
  17.         if ((conf->debug > 1) || (conf->debug < 0))
  18.             conf->debug = default_vrijednosti.debug;
  19.  
  20.         if (conf->max_rez == 0)
  21.             conf->max_rez = default_vrijednosti.max_rez;
  22.  
  23.         fclose(fp);
  24.     }
  25.     else
  26.         memcpy(conf, &default_vrijednosti, sizeof(podesavanja));
  27. }
  28.  
  29. void sacuvaj_podesavanja(podesavanja conf)
  30. {
  31.     FILE *fp = fopen(CONF_FILE, "wb");
  32.  
  33.     if(fp != NULL)
  34.     {
  35.         fwrite(&conf, sizeof(podesavanja), 1, fp);
  36.         fclose(fp);
  37.     }
  38. }
  39.  
  40. void podesavanja_meni()
  41. {
  42.     int selected = 0;
  43.     podesavanja conf;
  44.     ucitaj_podesavanja(&conf);
  45.     int in_menu = TRUE;
  46.  
  47.     while(in_menu)
  48.     {
  49.         int width, height;
  50.         getmaxyx(stdscr, height, width);
  51.  
  52.         int x = (width-30)/2;
  53.         int y = (height-16)/2;
  54.         clear();
  55.  
  56.         mvaddstr(y++, (width-strlen("HANGMAN :: Podesavanja"))/2, "HANGMAN :: Podesavanja");
  57.         y++;
  58.  
  59.         if (selected == 0)
  60.             attron(A_REVERSE);
  61.  
  62.         mvaddstr(y, x, "Debug:");
  63.         attroff(A_REVERSE);
  64.  
  65.         switch(conf.debug)
  66.         {
  67.             case 0:
  68.                 mvaddstr(y++, x+25, "False");
  69.             break;
  70.  
  71.             case 1:
  72.                 mvaddstr(y++, x+25, "True");
  73.             break;
  74.         }
  75.  
  76.         if (selected == 1)
  77.             attron(A_REVERSE);
  78.  
  79.         mvaddstr(y, x, "Maksimalni rezultat:");
  80.         attroff(A_REVERSE);
  81.  
  82.         switch (conf.max_rez)
  83.         {
  84.             case 0:
  85.                 mvprintw(y++, x+25, "Neograniceno");
  86.             break;
  87.  
  88.             default:
  89.                 mvprintw(y++, x+25, "%d", conf.max_rez);
  90.         }
  91.  
  92.         if (selected == 2)
  93.             attron(A_REVERSE);
  94.         y++;
  95.  
  96.         mvaddstr(y++, x, "Sacuvaj podesavanja");
  97.         attroff(A_REVERSE);
  98.  
  99.         if(selected == 3)
  100.             attron(A_REVERSE);
  101.  
  102.         mvaddstr(y++, x, "Default podesavanja");
  103.         attroff(A_REVERSE);
  104.  
  105.         refresh();
  106.  
  107.         int c = getch();
  108.         if (c == KEY_UP)
  109.             selected--;
  110.             else if (c == KEY_DOWN)
  111.                 selected++;
  112.             else if ((c == KEY_RIGHT) || (c == ' ') || (c == '\n'))
  113.             {
  114.                 switch(selected)
  115.                 {
  116.                     case 0:
  117.                         conf.debug++;
  118.                         if (conf.debug > 1)
  119.                             conf.debug = 0;
  120.                         break;
  121.  
  122.                     case 1:
  123.                         conf.max_rez+=1;
  124.                         conf.max_rez%=110;
  125.  
  126.                         if (conf.max_rez >= 101)
  127.                             conf.max_rez = 0;
  128.                         break;
  129.  
  130.                     case 2:
  131.                         sacuvaj_podesavanja(conf);
  132.                         in_menu=FALSE;
  133.                         break;
  134.  
  135.                     case 3:
  136.                         memcpy(&conf, &default_vrijednosti, sizeof(podesavanja));
  137.                 }
  138.             }
  139.  
  140.             else if (c == KEY_LEFT)
  141.             {
  142.                 switch(selected)
  143.                 {
  144.                     case 0:
  145.                         conf.debug--;
  146.                         if (conf.debug < 0)
  147.                             conf.debug = 1;
  148.                         break;
  149.  
  150.                     case 1:
  151.                         if (conf.max_rez==0)
  152.                             conf.max_rez=100;
  153.                         else
  154.                             conf.max_rez-=1;
  155.  
  156.                         if (conf.max_rez >= 101)
  157.                             conf.max_rez = 0;
  158.                         break;
  159.  
  160.                 }
  161.             }
  162.                 if (selected < 0)
  163.                     selected += 6;
  164.                 else
  165.                     selected %= 6;
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement