All pastes #146926 Raw Edit

Untitled

public text v1 · immutable
#146926 ·published 2006-08-23 23:52 UTC
rendered paste body
/*
 * Copyright (C) 2006 Terry Stenvold
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>

#include "pz.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define PICK "Pick!"
#define WIN "GOOD PICK!"
#define LOST "MISS!"
#define UP "TIMES UP!"
#define LOCK "LOCKED!"

#ifdef IPOD
#define SAVE "/usr/lib/nose/.save"
#else
#define SAVE "/.save"
#endif


static PzModule *module; 
static PzConfig *config;

static TWindow *window;

static TWidget *wid;
static char file[10];
static ttk_surface image[3];
static int imgw, imgh,imgw2, imgh2;;
static int w, h, bpp;
static int level=0, nwlevel=0;
static int offx=15, offy=0, trip=0, stptmr=0, over=0, tmr, pp, spp, nostrol=0;
static ttk_color bg;

static const char* level_options[] = { "1", "2", "3", "4","5", NULL };
//static const char* type_options[] = { "Normal", "Nose Move", "Nose Spin", NULL };

void cleanup_nose() 
{
offx=15;
offy=0;
stptmr=0;
over=0;
tmr=0;
}

void level_check()
{
	FILE *input;
	if ((input = fopen(SAVE, "r")) == NULL)
	{
		perror(SAVE);
		return;
	}
	fscanf(input, "%d", &level); 
	fclose(input);
	//printf("%d", level);

}

void level_write()
{

   if (nwlevel>level){
	FILE *output;
	if ((output = fopen(SAVE, "w")) == NULL)
	{
		perror(SAVE);
		return;
	}
	fprintf(output, "%d", nwlevel); 
	fclose(output);
	//printf("%d", nwlevel);
	}

}

void tmr_check()
{

	if (tmr>=300) {over=3; stptmr=5;}
	  
}

void check_pos()
{
 	if (offx>=24&&offx<=27) {over=1;}
	 else if(offx>=44&&offx<=47) {over=1;}
	  else over=2;
	
	if (over==1&&spp==0){nwlevel=pp+1; level_write();}
	 else if (over==1&&spp==1){nwlevel=5+pp+1; level_write();}
	 else if (over==1&&spp==2){nwlevel=10+pp+1; level_write();}

	stptmr=3;
}

void move_hand()
{
	int spd=1;

	switch(pp){
		case 0: spd*=1; break;
		case 1: spd*=2; break;
		case 2: spd*=3; break;
		case 3: spd*=5; break;
		case 4: spd*=8; break;
		}

	if(trip==0){offx+=1*spd;}
	 else if(trip==1){offx-=1*spd;}

	if(offx<15){trip=0;}
	 else if(offx>60){trip=1;}

	  
}

void pick()
{
 	if (offy>=45) {stptmr=2;}
 	 else offy+=5;

}

void draw_nose (PzWidget *wid, ttk_surface srf) 
{

	bg = ttk_makecol(WHITE); 
	ttk_fillrect (srf, 0, 0, wid->win->w, wid->win->h, bg); //white background

	if (pp>level)	
		{ttk_text (srf, ttk_menufont, 2, 2, ttk_makecol (BLACK), LOCK);}
	 else if (spp==1&&(5+pp)>level)
		{ttk_text (srf, ttk_menufont, 2, 2, ttk_makecol (BLACK), LOCK);}
	 else if (spp==2&&(10+pp)>level)
		{ttk_text (srf, ttk_menufont, 2, 2, ttk_makecol (BLACK), LOCK);}

else if (spp==0){

 ttk_blit_image (image[0], srf, wid->x + (wid->w - imgw) / 2, wid->y + (wid->h - imgh) / 2 - 20);

 ttk_blit_image (image[1], srf, wid->x + (wid->w - imgw2) / 2 + ((offx-30)), wid->y + (wid->h - imgh2) / 2 + 50 - offy );

 if (nostrol==1||over==1){
 ttk_blit_image (image[2], srf, wid->x + (wid->w - imgw) / 2, wid->y + (wid->h - imgh) / 2 - 20);}

 	switch(over){
	 	case 0: ttk_text (srf, ttk_menufont, 2, 2, ttk_makecol (BLACK), PICK); break;
		case 1:  ttk_text (srf, ttk_menufont, 2, 2, ttk_makecol (BLACK), WIN); break;
		case 2: ttk_text (srf, ttk_menufont, 2, 2, ttk_makecol (BLACK), LOST); break;
		case 3: ttk_text (srf, ttk_menufont, 2, 2, ttk_makecol (BLACK), UP); break;
		}

    ttk_line (srf, w - 35 , 2, w - 35 + (tmr/10), 2, ttk_makecol (BLACK));
    ttk_line (srf, w - 5 , 2, w - 5, 5, ttk_makecol (BLACK));
	}

else if (spp==1){

 ttk_blit_image (image[0], srf, wid->x + (wid->w - imgw) / 2 + ((offx-30)), wid->y + (wid->h - imgh) / 2 - 20);

 ttk_blit_image (image[1], srf, wid->x + (wid->w - imgw2) / 2, wid->y + (wid->h - imgh2) / 2 + 50 - offy );

 if (nostrol==1||over==1){
 ttk_blit_image (image[2], srf, wid->x + (wid->w - imgw) / 2 + ((offx-30)), wid->y + (wid->h - imgh) / 2 - 20);}

 	switch(over){
	 	case 0: ttk_text (srf, ttk_menufont, 2, 2, ttk_makecol (BLACK), PICK); break;
		case 1:  ttk_text (srf, ttk_menufont, 2, 2, ttk_makecol (BLACK), WIN); break;
		case 2: ttk_text (srf, ttk_menufont, 2, 2, ttk_makecol (BLACK), LOST); break;
		case 3: ttk_text (srf, ttk_menufont, 2, 2, ttk_makecol (BLACK), UP); break;
		}

    ttk_line (srf, w - 35 , 2, w - 35 + (tmr/10), 2, ttk_makecol (BLACK));
    ttk_line (srf, w - 5 , 2, w - 5, 5, ttk_makecol (BLACK));
	}

 

}

static int event_nose (PzEvent *e) 
{

int ret = TTK_EV_CLICK;

switch(e->type) {

	case PZ_EVENT_SCROLL: //scroll function
	  TTK_SCROLLMOD( e->arg, 10);
		if( e->arg > 0){break;} //forward scroll
		else {break;}		//backwards scroll

	case PZ_EVENT_BUTTON_DOWN:   
		switch (e->arg) {
		case PZ_BUTTON_ACTION: //center / action button
			stptmr=1;
			break;

		case PZ_BUTTON_MENU:   //menu button
			break;

		case PZ_BUTTON_PLAY:	//play button
			cleanup_nose();
			level_check();
			break;

		case PZ_BUTTON_NEXT:    //forward button
			break;

		case PZ_BUTTON_PREVIOUS: //previous button
			break;

		case PZ_BUTTON_HOLD :
			cleanup_nose();
			level_check();
			pz_close_window(e->wid->win);	

			break;
		default:
			ret = TTK_EV_UNUSED; //do nothing if not buttons pressed
		}
		break;

	case PZ_EVENT_TIMER: //timer function 
		if (stptmr==0){move_hand(); tmr++; tmr_check();}
		else if(stptmr==1){pick(); 
	 		if (offx>=23&&offx<=26) {nostrol=1;}
		  	else if(offx>=43&&offx<=46) {nostrol=1;}
	  		else nostrol=0;}
		else if(stptmr==2){check_pos();}
		wid->dirty++;
		break;

	default:
		ret = TTK_EV_UNUSED; //do nothing
		break;

	}
	return ret;

}

PzWindow *new_nose_window()
{
	int i;
	
	for(i=0;i<=2;i++){
	sprintf(file, "%d.gif", i);
	image[i]=ttk_load_image (pz_module_get_datapath (module, file));}

	ttk_surface_get_dimen (image[0], &imgw, &imgh);
	ttk_surface_get_dimen (image[1], &imgw2, &imgh2);

	w =ttk_screen->w; //gets screen width
	h =ttk_screen->h; //gets screen hieght
 	bpp =ttk_screen->bpp; //gets colour depth

    window = pz_new_window ("NosePick", PZ_WINDOW_NORMAL); 
    wid = pz_add_widget(window, draw_nose, event_nose); 

	pp = pz_get_int_setting(config,1); 
	//spp = pz_get_int_setting(config,2); 
	pz_widget_set_timer(wid, 1);
	//printf("pp %d %d ",pp,spp);

	level_check();

    return pz_finish_window (window);
}

static int save_nose_config(TWidget *this, int key, int time)

{

	if(key == TTK_BUTTON_MENU){

		pz_save_config(config);
	}

	return ttk_menu_button(this, key, time);

}

void init_nose() 
{
 
    module = pz_register_module ("nose", NULL); 
    config = pz_load_config(pz_module_get_cfgpath(module, "nose.conf"));
    pz_menu_add_action ("/Extras/Games/NosePick/Play", new_nose_window);
    pz_menu_add_setting("/Extras/Games/NosePick/Level", 1, config, level_options);
   // pz_menu_add_setting("/Extras/Games/NosePick/Type", 2, config, type_options);
 ((TWidget *)pz_get_menu_item("/Extras/Games/NosePick")->data)->button = save_nose_config;


}

PZ_MOD_INIT (init_nose)