/* 
    Spectrum Analyzer
    (C) 2007 Jason Hunt
    GPL

    file: main.cpp
*/


#include <windows.h>
#include <stdio.h>

#include "main.h"
#include "sound.h"
#include "window.h"
#include "display.h"
#include "input.h"
#include "debug.h"


HWND main_win = NULL;              // keep track of the main window
HWND system_hwnd = NULL;
HDC main_dc = NULL;

unsigned int second_count = 0;

/* ran when window is created */
void system_startup(HWND hwnd)
{
    reset_debug();
    
    add_debug("Program start", "");

    system_hwnd = hwnd;

    start_sound();
    
    SetTimer(hwnd, ID_DISPLAY, 50, NULL);
    SetTimer(hwnd, ID_SECOND, 1000, NULL);
}
/* end of system_startup */



/* ran when window is closed */
void system_shutdown(void)
{
    add_debug("Program stop", "");

    stop_sound();

    PostQuitMessage(0);   
}
/* end of system_shutdown */


/* deal with the system timer */
void timer_message(WPARAM wParam)
{
    if (wParam == ID_DISPLAY) // display?
    {    
        display();
    }   else
    if (wParam == ID_SECOND)
    {
        second_count++;
    }   
}
/* end of timer message */


/* the message handler */
LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                 
    {
        case WM_CREATE:     system_startup(hwnd);    break;
        case WM_TIMER:      timer_message(wParam); break;
        case WM_DESTROY:    system_shutdown(); break;
        case WM_KEYDOWN:    process_key_down(wParam); break;
        case WM_KEYUP:      process_key_up(wParam); break;
        case WM_PAINT:      display();  break;
   
        default: return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return(0);
}
/* end of winproc */


/* winmain */
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // make the main window
    if (setup_window(hinstance, nCmdShow)) return(1);

    MSG messages;

    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }

    return(messages.wParam);
}
/* end of winmain */



