Changeset 33

Show
Ignore:
Timestamp:
01/14/08 06:33:51 (11 months ago)
Author:
charlie137
Message:

- Added switch fullscreen on/off
- Try to load data from standard directories first

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/guisterax/TODO

    r24 r33  
    11 
     2- pause screen 
    23- add documentation  
    34- Make collisions working at the universe border 
    4 - Score 
    55- beeing able to kill bouncing balls 
    66 
  • trunk/guisterax/src/display.d

    r32 r33  
    113113    static MainDisplay display; 
    114114public: 
     115    int width, height; 
     116    bool fullscreen = false; 
     117 
     118public: 
    115119    static MainDisplay opCall() { 
    116120        assert(display); 
     
    120124    this(int w = 640, int h = 480) { 
    121125        assert(!display); 
     126         
     127        width = w; 
     128        height = h; 
    122129         
    123130        DerelictSDL.load(); 
     
    130137        } 
    131138         
    132         Uint8 video_bpp = 32; 
    133         Uint32 videoflags = SDL_HWSURFACE; 
    134         // videoflags |= SDL_FULLSCREEN; 
     139        init_screen(); 
    135140         
    136         // SDL_GetVideoInfo().vfmt.BitsPerPixel; 
    137         // printf("VideoModeOK = %d\n", SDL_VideoModeOK(640, 480, 32, SDL_FULLSCREEN|SDL_SWSURFACE)); 
    138          
    139         // printf("set video mode to %d %d\n", w, h); 
    140         if ( (screen=SDL_SetVideoMode(w,h,video_bpp,videoflags)) == null ) 
    141         { 
    142             throw new Error(format("Couldn't set %dx%d video mode", w, h)); 
    143         } 
    144          
    145         // No the audio system 
     141        // Now the audio system 
    146142        int audio_rate = 22050; 
    147143        uint audio_format = AUDIO_S16SYS; 
     
    155151        // We set the singelton display 
    156152        display = this; 
     153    } 
     154     
     155    void init_screen() { 
     156        Uint8 video_bpp = 32; 
     157        Uint32 videoflags = SDL_HWSURFACE; 
     158        if (fullscreen) videoflags |= SDL_FULLSCREEN; 
     159        if ( (screen=SDL_SetVideoMode(width,height,video_bpp,videoflags)) == null ) 
     160        { 
     161            throw new Error(format("Couldn't set %dx%d video mode", width, height)); 
     162        } 
    157163    } 
    158164     
     
    193199    } 
    194200     
     201    void switch_fullscreen() { 
     202        fullscreen = !fullscreen; 
     203        init_screen(); 
     204    } 
     205     
    195206    bool[] getKeys() { 
    196207        int numkeys; 
  • trunk/guisterax/src/game.d

    r32 r33  
    4040class Menu : menu.Menu 
    4141{ 
    42     class Entry : menu.Entry 
    43     { 
    44         static Surface selected_surf; 
    45         char[] text; 
    46         void delegate() func; 
    47          
    48         static void s_init() { 
    49             selected_surf = new Surface("data/menu/selected.png"); 
    50         } 
    51          
    52         this(in char[] text, void delegate() func, in Vect pos) { 
    53             super(pos); 
    54             this.text = text.dup; 
    55             this.func = func; 
    56         } 
    57         override void draw(Display disp, in Vect pos, bool selected = false) { 
    58             if (selected) { 
    59                 selected_surf.draw(disp, pos + this.pos); 
    60             } 
    61             Font.font.draw(disp, text, pos + this.pos, true); 
    62         } 
    63          
    64         override void on_activate() { 
    65             func(); 
    66         } 
    67     }; 
    68      
     42    alias menu.TextEntry Entry; 
     43 
    6944    static void s_init() { 
    7045        Entry.s_init(); 
     
    7348    this(Vect pos = Vect()) { 
    7449        super(pos); 
    75         append(new Entry("NEW GAME", &new_game, Vect(0,32))); 
    76         append(new Entry("HELP", &help, Vect(0, 0))); 
    77         append(new Entry("HIGH SCORE", &high_score, Vect(0, -32))); 
    78         append(new Entry("QUIT", &quit, Vect(0,-64))); 
     50        int y = 64; 
     51        append(new Entry("NEW GAME", &new_game, Vect(0,y))); 
     52        append(new Entry("HELP", &help, Vect(0, y-=32))); 
     53        append(new Entry("SWITCH FULL SCREEN", &switch_fullscreen, Vect(0, y-=32))); 
     54        append(new Entry("HIGH SCORE", &high_score, Vect(0, y-=32))); 
     55        append(new Entry("QUIT", &quit, Vect(0,y-=32))); 
    7956    } 
    8057     
     
    9269        Game g = new Game(); 
    9370        g.high_score(); 
     71    } 
     72     
     73    void switch_fullscreen() { 
     74        MainDisplay().switch_fullscreen(); 
    9475    } 
    9576     
     
    164145}; 
    165146 
     147 
     148class Pause : Universe { 
     149    alias menu.Menu Menu; 
     150    alias menu.TextEntry Entry; 
     151 
     152    Universe u; 
     153    Actor rest; 
     154     
     155    this(Universe u) { 
     156        super(); 
     157        this.u = u; 
     158         
     159        rest = new Actor(); 
     160        Menu menu = new Menu(); 
     161        menu.append(new Entry("RESUME", &resume, Vect(0,32))); 
     162        menu.append(new Entry("ABORT GAME", &abort, Vect(0,0))); 
     163        rest.append(menu); 
     164        append(rest); 
     165    } 
     166     
     167    void resume() { 
     168        kill(); 
     169    } 
     170     
     171    void abort() { 
     172         
     173        u.kill(); 
     174        kill(); 
     175    } 
     176     
     177    override void draw(Display disp, in Vect pos = Vect()) { 
     178        u.draw(disp, pos); 
     179        rest.draw(disp, pos + Vect(width / 2, height / 2)); 
     180    } 
     181} 
     182 
    166183class Quit : Exception 
    167184{ 
     
    257274        Font.font.draw(MainDisplay(), format("LEVEL %d", level), Vect(u.width / 2, u.height / 2)); 
    258275        MainDisplay().flip(); 
    259         Clock().tick(100); 
     276        Clock().tick(400); 
    260277        run(u); 
     278    } 
     279     
     280    void pause(Universe u) { 
     281        Pause pause = new Pause(u); 
     282        run(pause); 
     283        if (!u.alive) { // The game has been aborted 
     284            ship.kill(); 
     285        } 
    261286    } 
    262287     
     
    273298            while (SDL_PollEvent(&ev)) { 
    274299                if (ev.type == SDL_QUIT) {throw new Quit();} 
    275                 if (u.key(SDLK_ESCAPE)) {throw new Quit();} 
    276300            } 
    277301             
  • trunk/guisterax/src/menu.d

    r17 r33  
    2626import utils; 
    2727import display; 
     28import surface : Surface; 
     29import font : Font; 
    2830 
    2931public import actor; 
    3032 
    31 class Entry : Actor { 
     33class Entry : Actor 
     34
    3235public: 
    3336    Vect pos = Vect(); 
     
    4043        super(); 
    4144        this.pos = pos; 
     45    } 
     46} 
     47 
     48class TextEntry : Entry 
     49{ 
     50public: 
     51    static Surface selected_surf; 
     52    char[] text; 
     53    void delegate() func; 
     54     
     55    static void s_init() { 
     56        selected_surf = new Surface("data/menu/selected.png"); 
     57    } 
     58     
     59    this(in char[] text, void delegate() func, in Vect pos) { 
     60        super(pos); 
     61        this.text = text.dup; 
     62        this.func = func; 
     63    } 
     64    override void draw(Display disp, in Vect pos, bool selected = false) { 
     65        if (selected) { 
     66            selected_surf.draw(disp, pos + this.pos); 
     67        } 
     68        Font.font.draw(disp, text, pos + this.pos, true); 
     69    } 
     70     
     71    override void on_activate() { 
     72        func(); 
    4273    } 
    4374} 
  • trunk/guisterax/src/scores.d

    r32 r33  
    5656        // We open and parse the scores file 
    5757        try { 
    58             scope File file = new File("scores.txt"); 
     58            File file = open(); 
    5959            foreach(char[] line; file) { 
    6060                char[][] fields = split(line); 
     
    8585    } 
    8686     
     87    File open(FileMode mode = FileMode.In) { 
     88        foreach(path; var_paths("scores.hi")) { 
     89            try { 
     90                File file = new File("scores.hi", mode); 
     91                return file; 
     92            } 
     93            catch { 
     94            } 
     95        } 
     96        throw new Error("can't open high score file"); 
     97    } 
     98     
    8799    void write() { 
    88         scope File file = new File("scores.txt", FileMode.Out); 
     100        File file = open(FileMode.Out); 
    89101        foreach(score; scores) { 
    90102            file.writeLine(format("%d %s", score.score, score.name)); 
  • trunk/guisterax/src/shop.d

    r31 r33  
    153153        m_ship = ship; 
    154154         
    155         Menu menu = new Menu(Vect(0,0)); 
     155        Menu menu = new Menu(Vect(0,96)); 
    156156         
    157157        float dist = 32; 
  • trunk/guisterax/src/sound.d

    r10 r33  
    2727import std.string; 
    2828 
     29import utils : data_paths; 
     30 
    2931class Sound 
    3032{ 
     
    3436    this(in char[] file) { 
    3537        /* Load the WAV */ 
    36         m_chunk = Mix_LoadWAV(file); 
    37         if (m_chunk is null) { 
     38        foreach(path; data_paths(file)) { 
     39            m_chunk = Mix_LoadWAV(path); 
     40            if (m_chunk) break; 
     41        } 
     42        if (!m_chunk) { 
    3843            throw new Error(format("can't open wave file %s", file)); 
    3944        } 
  • trunk/guisterax/src/surface.d

    r31 r33  
    5656        assert(MainDisplay()); 
    5757         
    58         SDL_Surface* s1 = IMG_Load(toStringz(file)); 
    59         if (s1 is null) { 
    60             throw new Error(format("can't open image %s", file)); 
     58        SDL_Surface* s1 = null; 
     59         
     60        foreach(path; data_paths(file)) { 
     61            s1 = IMG_Load(toStringz(path)); 
     62            if (s1) break; 
    6163        } 
    62         // log(file, ' ', s1.h, ' ', s1.w); 
     64        if (!s1) throw new Error(format("can't open image %s", file)); 
     65         
    6366        surf = SDL_DisplayFormatAlpha(s1); 
    6467        SDL_FreeSurface(s1); 
  • trunk/guisterax/src/utils.d

    r10 r33  
    167167    return ret; 
    168168} 
     169 
     170char[][] data_paths(in char[] file) { 
     171    version(linux) { 
     172        return ["/usr/share/games/guisterax/" ~ file, file]; 
     173    } 
     174    else { 
     175        return [file]; 
     176    } 
     177} 
     178 
     179char[][] var_paths(in char[] file) { 
     180    version(linux) { 
     181        return ["/var/games/guisterax/" ~ file, file]; 
     182    } 
     183    else { 
     184        return [file]; 
     185    } 
     186} 
     187     
     188     
  • trunk/guisterax/src/wave.d

    r29 r33  
    3636import status; 
    3737import utils; 
     38import game : Game; 
    3839 
    3940import display; 
     
    122123            if (pressed_key(SDLK_k)) {append(new Bounce(safe_pos(128)));} 
    123124        } 
     125        if (pressed_key(SDLK_ESCAPE)) { 
     126            Game().pause(this); 
     127        } 
    124128    } 
    125129