Buchempfehlung
Windows System Programming
Windows System Programming
Das Kompendium liefert viele interessante Informationen zur Windows-Programmierung auf Englisch. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

Mahjonng-Patch für Tastatur

Uploader:MitgliedMilkFreeze
Datum/Zeit:11.12.2011 14:37:46

#include once "inc/all.bi"

dim shared as integer stones_left                   'contains the number of left stones
dim shared buttons as button_list                   'contains the buttons used in this game
dim shared as gfx_ressource_type ptr background_gfx
dim shared cursor as t_cursor = type(6,5) 'milkfreeze

declare sub init            ()
declare sub main            ()
declare sub redraw_all      ()

#include once "inc/misc.bi"

main() 'start main()

sub init()
    dim as double time1, time2
    if( SDL_Init(SDL_INIT_VIDEO) <> 0 ) then  'if SDL-init fails:
        end                                     'end
    end if

    sdl_video = SDL_SetVideoMode(w_width,w_height,32, SDL_DOUBLEBUF and SDL_HWSURFACE) 'or SDL_FULLSCREEN
    SDL_WM_SetCaption("Muhjongg", "")

    if( sdl_video = 0 ) then    'if SDL could not create a new window
        SDL_Quit                'then end!
        end
    end if

    gfx_init() 'see "misc.bi"
    'add the buttons:
    add_button("New Game",4,4,gfx_get_ptr("button:new_game"),@new_game, @buttons)
    add_button("Quit",200,4,gfx_get_ptr("button:quit"),@really_quit, @buttons)
    add_button("Hint",140,4,gfx_get_ptr("button:hint"),@hint, @buttons, true)

    'font-stuff
    TTF_Init()
    loadfont ("FreeSans.ttf", "fsans", 14, 1)
end sub

sub main()
    dim new_cursor as t_cursor'= type(6,5)'milkfreeze
    dim as stone_list ptr marked, temp
    dim as button_type ptr hovered, hovered_old
    Dim As SDL_Event event                      'contains the SDL-event
    init()
    stones_left = fill_board()
    redraw_all() 'draw board + buttons
    mutexlock(xm)
    sdl_flip(sdl_video)
    mutexunlock(xm)
    start_time = now
    threadcreate(cast(any ptr,@time_thread()))
    Do
        if SDL_WaitEvent ( @event ) then 'wait for an event
            Select Case  as const event.Type
            case SDL_QUIT_:
                end 0
            case SDL_KeyDown:
                'MilkFreeze:
                select case event.key.keysym.sym
                case SDLK_ESCAPE
                    quit(0)

                case SDLK_N
                    new_game(0)
                case SDLK_F1, SDLK_H
                    hint()
                case SDLK_UP
                    new_cursor = cursor
                    if (new_cursor.y > 1) then
                        new_cursor.y -= 1
                    end if
                case SDLK_DOWN
                    new_cursor = cursor
                    if (new_cursor.y < ubound(board,2)-2) then
                        new_cursor.y += 1
                    end if
                case SDLK_RIGHT
                    new_cursor = cursor
                    if (new_cursor.x < ubound(board,1)-1) then
                        new_cursor.x += 1
                    end if
                case SDLK_LEFT
                    new_cursor = cursor
                    if (new_cursor.x > 1) then
                        new_cursor.x -= 1
                    end if

                case SDLK_RETURN, SDLK_SPACE
                    if board(cursor.x, cursor.y).count > 0 then
                        temp = check_stones(cursor)
                        if temp <> 0 then
                            if (marked = 0) then 'if no stone marked
                                marked = temp 'set temp as marked:
                                marked->last->cursor = false
                                marked->last->marked = true
                            elseif temp <> marked then 'if a stpne marked and the marked is not the same as temp
                                marked->last->marked = false 'un-mark the stone
                                marked->last->cursor = false
                                if marked->last->typ = temp->last->typ then 'do the stones match?
                                    marked->del_last() ' delete stone1
                                    temp->del_last()   ' delete stone2
                                    marked = 0          ' clear "marked"
                                    stones_left -= 2
                                    if stones_left = 0 or check_solvable() = false then
                                        redraw_all()
                                        mutexlock(xm)
                                        pause = true
                                        mutexunlock(xm)
                                        finish()
                                    end if
                                else
                                    marked = temp       'set the last-clicked-stone as the marked one
                                    marked->last->marked = true  'for drawing as marked
                                end if
                            end if
                            redraw_all()                'redraw the whole game
                            temp = 0                    'clean temp
                        end if
                    end if
                end select
                if (new_cursor.x <> cursor.x OR new_cursor.y <> cursor.y ) then
                    unset_cursor(cursor.x, cursor.y)
                    mutexlock(xm)
                    cursor = new_cursor
                    mutexunlock(xm)
                    redraw_all()
                end if
                'ende MilkFreeze
            case SDL_MouseMotion
                hovered = check_buttons(event.motion.x, event.motion.y,@buttons) 'get the "hovered" buttons
                if hovered <> 0 and hovered <> hovered_old then
                    hovered->hover = true 'to draw the button as hovered
                    if hovered_old <> 0 then hovered_old->hover = false 'reset the old hovered-one
                    hovered_old = hovered
                    redraw_all()
                elseif hovered_old <> 0 and hovered = 0 then
                    hovered_old->hover = false ' "unhover"
                    hovered_old = 0 'delete hovered_old
                    redraw_all()
                    if (cursor.x = 0 or cursor.y = 0) then
                        cursor = type(2,2)
                        mutexlock(xm)
                        gfx_draw_by_ptr(cursor_gfx, cursor.x * 60 - 60 + 32, cursor.y * 80 -80 + 32)
                        sdl_flip(sdl_video)
                        mutexunlock(xm)
                    end if
                'MilkFreeze:
                elseif (new_cursor.x = 0 and new_cursor.y = 0) then
                    new_cursor.y = int((event.motion.y - 32) / 80) +1
                    new_cursor.x = int((event.motion.x - 32) / 60) +1
                else
                    new_cursor.y = int((event.motion.y - 32) / 80) +1
                    new_cursor.x = int((event.motion.x - 32) / 60) +1
                    if (new_cursor.x <> cursor.x OR new_cursor.y <> cursor.y ) then
                        if (new_cursor.x > 0 and new_cursor.y > 0) then
                            unset_cursor(cursor.x, cursor.y)
                            cursor = new_cursor
                            set_cursor(event.motion.x, event.motion.y)
                            redraw_all()
                        end if
                    end if
                'Ende MilkFreeze
                end if
            Case SDL_MOUSEBUTTONDOWN
                If (event.button.button = SDL_BUTTON_LEFT) then
                    if hovered <> 0 Then 'if a button hovered then...
                        hovered->hover = false
                        refresh_buttons(@buttons)
                        sdl_flip(sdl_video)
                        hovered->action() '..."click"
                        redraw_all()
                    end if
                    temp = check_stones(event.motion.x, event.motion.y) 'get clicked stone
                    if temp <> 0 then
                        if (marked = 0) then 'if no stone marked
                            marked = temp 'set temp as marked:
                            marked->last->cursor = false
                            marked->last->marked = true
                        elseif temp <> marked then 'if a stpne marked and the marked is not the same as temp
                            marked->last->marked = false 'un-mark the stone
                            marked->last->cursor = false
                            if marked->last->typ = temp->last->typ then 'do the stones match?
                                marked->del_last() ' delete stone1
                                temp->del_last()   ' delete stone2
                                marked = 0          ' clear "marked"
                                stones_left -= 2
                                if stones_left = 0 or check_solvable() = false then
                                    redraw_all()
                                    mutexlock(xm)
                                    pause = true
                                    mutexunlock(xm)
                                    finish()
                                end if
                            else
                                marked = temp       'set the last-clicked-stone as the marked one
                                marked->last->marked = true  'for drawing as marked
                            end if
                        end if
                        redraw_all()                'redraw the whole game
                        temp = 0                    'clean temp
                    end if
                end if
            End Select
        end if
        Sleep 1,1
    Loop
end sub

sub redraw_all()
    'you shouldnt read that:
    '(you have been warned)
    mutexlock(xm)
    gfx_draw_by_ptr(background_gfx, 0,0)  'draw background
    refresh_buttons(@buttons)             'draw buttons

    if board(cursor.x, cursor.y).count > 0 then
        board(cursor.x, cursor.y).last->cursor = true
    else
        gfx_draw_by_ptr(cursor_gfx, cursor.x * 60 - 60 + 32, cursor.y * 80 -80 + 32)
    end if

    draw_stones()                         'draw stones
    gfx_draw_by_ptr(box_gfx, 0,w_height-32)
    dim as string foo = string(3 -len(str(stones_left)), "0") & stones_left
    draw_string ("fsans",11, w_height -25,  "Verbleibend: " & foo, &h000000)
    time_box_draw()
    sdl_flip(sdl_video)                   'flip the screen (show the drawn stuff)
    mutexunlock(xm)
end sub