Sorry for the downtime, welcome back!!

Creating KG-Chart for Mac

Forum and Craft Related Topics...
User avatar
Icelandic Hitman
Rank 6 - Tanooki Mario
Rank 6 - Tanooki Mario
Posts: 285
Joined: Wed Feb 25, 2009 10:04 am
Contact:

Re: Creating KG-Chart for Mac

Post by Icelandic Hitman »

Hi,

Thought I would jump in. First a little background on me.
I've been in the Games Industry for 6 years. I've been a Programmer at Sony Computer Entertainment Europe Cambridge, I have 7 games to my name. Basically I am a hardcore c/++ programmer. I have just started looking at iOS dev and hence Mac dev and obj-c.

I was having the same problems FouFou was having with your code, below I have made some fixes which I hope should get it working for FouFou as well. Main point is the txt files now need a .txt extension which is how you download them from here, but its easy enough to change that back.
Spoiler

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


char code[454][7];
char name [454][26];
char haveslist [454][11];
char wrapped [454][15];
char input [25]				= "";
char onbobbin[10]			= "on bobbin";			//Inits the global one
char notonbobbin[14]		= "not on bobbin";
char all[4]					= "all";
char have[6]				= "have";
char donthave[12]			= "don't have";
enum commands 
{
	NoInput = 0
	,OnBobbin
	,NotOnBobbin
	,Have
	,DontHave
	,All
	,Blank
};
int i;
//int f; //this is a global which you you then pass into a function, which then has a local version that shadows this var. Makes it very confusing.
int h;
int r;
int len;
int arrays (enum commands  f, int *hPtr);
void edit (int i);



int main (int argc, const char * argv[])
{
	int h;
	//for (r=0; r=100000; r++) //you have made a for loop with a stupidly large count to endless loop?
	while(1) //this will loop for ever, until you return or break out of it
	{
		fpurge(stdin);
		//these definition are showdowing the global ones above, so any code in this func gets a valid result, 
		//any code out of this func references onbobbin it will get random data.
		/*
		char onbobbin[] = "on bobbin";
		char notonbobbin[] = "not on bobbin";
		char all[] = "all";
		char have[] = "have";
		char donthave[] = "don't have";
		*/
		
		if (r!=0)
		{
			printf("\n-----------\n");
		}
		
		printf("\nEnter code, name of color, \"on bobbin\", \"not on bobbin\", \"have\", \"don't have\" or \"all\" (q=quit):\n ");
		fgets(input, 25, stdin);
		
		for( int j = 0 ; j < len ; ++j )
		{
			input[j] = tolower(input[j]); //make sure all chars are lowercase for the following compares, so Have == have
		}
		
		enum commands f = NoInput;
		
		if (*input == 'q') 
		{
			printf("Quitting.");
			return (0);
		}
		else if (*input == '\0')
		{
			f = Blank;
		}
		else if (strcmp(input, &onbobbin[0]) == 0) //this func take char*,char* onbobbin is char, &get the adress of, [0] gets the first element in memory
		{
			f = OnBobbin;
		}
		else if (strcmp(input, &notonbobbin[0]) == 0) 
		{
			f = NotOnBobbin;
		}
		else if (strcmp(input, &have[0]) == 0) 
		{
			f = Have;
		}
		else if (strcmp(input, &donthave[0]) == 0) 
		{
			f = DontHave;
		}
		else if (strcmp(input, &all[0]) == 0) 
		{
			f = All;
		}
		else 
		{
			f = NoInput;
		}
		
		arrays(f, &h);
		
		if (h!=1 && f==0) 
		{
			edit(i);			
		}
		else if (h == 1 && f==0) 
		{
			printf("Invalid entry.\n");
		}
		
	}
	
	printf("-----------\n");
	
	return 0;
	
}

int arrays (enum commands  f, int *hPtr) 
{
	char compare5[] = "don't have";
	
    FILE *namelist =	fopen("../../namelist.txt", "r+");
    FILE *codelist =	fopen("../../codelist.txt", "r+");
    FILE *wrappedlist = fopen("../../wrapped.txt", "r+");
    FILE *havelist =	fopen("../../havelist.txt", "r+");
	
    fseek(namelist, 0, SEEK_SET);
    fseek(codelist, 0, SEEK_SET);
    fseek(wrappedlist, 0, SEEK_SET);
    fseek(havelist, 0, SEEK_SET);
	
    for (i=0; i<=453; i++) 
	{
		fgets(wrapped[i], 15, wrappedlist);
		fgets(code[i], 7, codelist);
		fgets(name[i], 26, namelist);
		fgets(haveslist[i], 12, havelist);
		
		/*len = strlen(code[i]);
		code[i][len - 1] = '\0';
		len = strlen(name[i]);
		name[i][len - 1] = '\0';
		len = strlen(wrapped[i]);
		wrapped[i][len - 1] = '\0';
		len = strlen(haveslist[i]);
		haveslist[i][len - 1] = '\0';
		*/
		//you are repeating checks here which makes the if statements more complicated and the code harder to read, while taking 2 as long.
		if( f == OnBobbin ) //if f ==1
		{
			//if( strncmp(&input[0], &wrapped[i],1 ) this is comparing a single char?
			if( input[0] == wrapped[i][0] ) 
			{
				
				printf("\n");
				if (i >= 3 ) 
				{
					printf("#");
				}
				printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
				
				if( i == 454 )
				{
					fclose(wrappedlist);
					fclose(namelist);
					fclose(codelist);
					fclose(havelist);
					*hPtr=0;
					return(i,f); //this returns f, as you can only return one thing in c
				}
			}
		}
		else if ( f == NotOnBobbin )
		{
			if( input[0] == wrapped[i][0] ) 
			{
				
				printf("\n");
				if (i >= 3 ) 
				{
					printf("#");
				}
				printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
				
				if( i == 454 )
				{
					fclose(wrappedlist);
					fclose(namelist);
					fclose(codelist);
					fclose(havelist);
					*hPtr=0;
					return(i,f); //this returns f, as you can only return one thing in c
				}
			}		
		}
		else if( f == NoInput )
		{
			if( ( strcasecmp( &input[0], &code[i][0] ) == 0 ) || ( strcasecmp( &input[0], &name[i][0]) == 0 ) )
			{
				printf("\n");
				if ( i >= 3 ) 
				{
					printf("#");
				}	
				
				if( haveslist[i][0] == compare5[0] )
				{
					printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
				}
				else
				{
					printf("%s, %s\n-----------\n%s\n",code[i],name[i],haveslist[i]);
				}
				
				fclose(wrappedlist);
				fclose(namelist);
				fclose(codelist);
				fclose(havelist);
				f=0;
				*hPtr=0;
				return(i, f);	
			}
		}
		else if ( f == All )
		{
			printf("\n");
			if (i>=3) 
			{
				printf("#");
			}
			
			if( haveslist[i][0] == compare5[0] )
			{
				printf("%s, %s\n-----------\n%s\n",code[i],name[i],haveslist[i]);
			}
			else
			{
				printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);	
			}
			
			if( i == 454 )
			{
				fclose(wrappedlist);
				fclose(namelist);
				fclose(codelist);
				fclose(havelist);
				*hPtr=0;
				return(i, f);
			}

		}
		else if ( f == Have )
		{
			if( input[0] == haveslist[i][0] )
			{
				printf("\n");
				if (i>=3) 
				{
					printf("#");
				}
				printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
				if( i == 454 )
				{
					fclose(wrappedlist);
					fclose(namelist);
					fclose(codelist);
					fclose(havelist);
					*hPtr=0;
					return(i, f);
				}
			}
		}
		else if ( f == DontHave )
		{
			if ( strcmp( &input[0], &haveslist[i][0] ) == 0 )	
			{
				printf("\n");
				if (i>=3) 
				{
					printf("#");
				}
				printf("%s, %s\n-----------\n%s\n",code[i],name[i],haveslist[i]);
				
				if( i == 454 )
				{
					fclose(wrappedlist);
					fclose(namelist);
					fclose(codelist);
					fclose(havelist);
					*hPtr=0;
					return(i, f);
				}
			}
		}
		else if ((i==453) && ((strcasecmp(&input[0], &code[i][0])!=0)||(strcmp(&input[0], &name[i][0])!=0)))
		{
			fclose(wrappedlist);
			fclose(namelist);
			fclose(codelist);
			fclose(havelist);
			*hPtr=1;
			return(i, f);
		}
    }
	
	return (0);
}



void edit (int i){
	char ans[5];
	char ans2[5];
	char yes[] = "yes";
	char no[] = "no";
	char compare1[] = "on bobbin\b\b\b\b";
	char compare2[] = "not on bobbin";
	char compare3[] = "have\b\b\b\b\b\b";
	char compare4[] = "don't have";
	char wrapedit[] = "wrapped";
	char haveedit[] = "have";
	char editing[9];
	
	FILE *wrappedlist = fopen("../../wrapped.txt", "r+");
	FILE *havelist = fopen("../../havelist.txt", "r+");
	
	fseek(wrappedlist, i*14, SEEK_SET);
	fseek(havelist, i*11, SEEK_SET);
	
	fpurge(stdin);
	
	printf("Edit? (\"yes\"/\"no\")\n");
	fgets(ans, 5, stdin);
	len = strlen(ans);
	ans[len - 1] = 0;   
	
	if ((strcmp(ans, yes) == 0) && (strncmp(haveslist[i], compare4, (size_t)1) == 0)) {
		fpurge(stdin);
		
		printf("Switch \"don't have\" to \"have\"? (\"yes\"/\"no\")\n");
		fgets(ans2, 5, stdin);
		len = strlen(ans2);
		ans2[len -1] = 0;
		
		if (strcmp(ans2, yes)==0) {
			printf("Switched \"don't have\" to \"have\".");
			fputs(compare3, havelist);
			fclose(wrappedlist);
			fclose(havelist);
			return;
		}
		else if(strcmp(ans2, no)==0){
			
			printf("Not switching \"don't have\" to \"have\".");
			fclose(wrappedlist);
			fclose(havelist);
			return;
		}
		
		printf("Invalid entry1.");
		fclose(wrappedlist);
		fclose(havelist);
		return;
		
	}
	
	else if ((strcmp(ans, yes)==0) && (strncmp(haveslist[i], compare3, (size_t)1) == 0)) {
		
		printf("Edit \"have\" or \"wrapped\"?\n");
		fpurge(stdin);
		fgets(editing, 9, stdin);
		len = strlen(editing);
		editing[len - 1] = 0;
		
		if (strcmp(editing, wrapedit)==0) {
			
			if (strncmp(wrapped[i], compare1, (size_t)1)==0) {
				
				printf("Switch \"on bobbin\" to \"not on bobbin\"? (\"yes\"/\"no\")\n");
				fgets(ans2, 5, stdin);
				len = strlen(ans2);
				ans2[len - 1] = 0;
				
				if (strcmp(ans2, yes)==0) {
					printf("Switched \"on bobbin\" to \"not on bobbin\".");
					fputs(compare2, wrappedlist);
					fclose(wrappedlist);
					fclose(havelist);
					return;
				}
				else if(strcmp(ans2, no)==0){
					
					printf("Not switching \"on bobbin\" to \"not on bobbin\".");
					fclose(wrappedlist);
					fclose(havelist);
					return;
				}
				printf("Invalid entry2.");
				fclose(wrappedlist);
				fclose(havelist);
				return;
			}
			else if(strncmp(wrapped[i], compare2, (size_t)1)==0){
				
				
				printf("Switch \"not on bobbin\" to \"on bobbin\"? (\"yes\"/\"no\")\n");
				fgets(ans2, 5, stdin);
				len = strlen(ans2);
				ans2[len - 1] = 0;
				if (strcmp(ans2, yes)==0) {
					printf("Switched \"not on bobbin\" to \"on bobbin\".");
					fwrite(compare1, (size_t)strlen(compare1), 1, wrappedlist);
					fclose(wrappedlist);
					fclose(havelist);
					return;
				}
				else if(strcmp(ans2, no)==0){
					printf("Not switching \"not on bobbin\" to \"on bobbin\".");
					fclose(wrappedlist);
					fclose(havelist);
					return;
				}
				
				printf("Invalid entry.");
				fclose(wrappedlist);
				fclose(havelist);
				return;
			}
			
			printf("You don't want to edit wrapped apparently.");
			fclose(wrappedlist);
			fclose(havelist);
			return;
		}
		else if(strcmp(editing, haveedit)==0){
			
			if (strncmp(haveslist[i], compare3, 1) == 0){
				
				printf("Switch \"have\" to \"don't have\"? (\"yes\"/\"no\")\n");
				fgets(ans2, 5, stdin);
				len = strlen(ans2);
				ans2[len - 1] = 0;
				if (strcmp(ans2, yes)==0) {
					printf("Switched \"have\" to \"don't have\".");
					fputs(compare2, wrappedlist);
					fputs(compare4, havelist);
					fclose(wrappedlist);
					fclose(havelist);
					return;
				}
				else if(strcmp(ans2, no)==0){
					printf("Not switching \"have\" to \"don't have\".");
					fclose(wrappedlist);
					fclose(havelist);
					return;
				}
				printf("Invalid entry.");
				fclose(wrappedlist);
				fclose(havelist);
				return;
			}
			printf("Invalid entry.");
			fclose(wrappedlist);
			fclose(havelist);
			return;
		}
		printf("Invalid Entry.");
		
		fclose(wrappedlist);
		fclose(havelist);
		
		return;
	}
	else if(strcmp(ans, no)==0){
		printf("Not editing.");
      fclose(wrappedlist);
      fclose(havelist);
      return;
   }
   printf("Invalid entry.");
   fclose(havelist);
   fclose(wrappedlist);
   return;
}
I have made a couple of changes to the code, which I have commented, I hope some of the changes aren't too drastic that you can still see the logic steps I have take with parts of the code. Any probs give me a shout.

Tested on Intel Mac OS X 10.6.3
Thread Tracker for iPhone/iPod touch/iPad: http://touchscreencraft.wordpress.com
Etsy items for sales and charts : http://www.etsy.com/shop/touchscreencraft
DA : http://oziphantom.deviantart.com/

User avatar
Icelandic Hitman
Rank 6 - Tanooki Mario
Rank 6 - Tanooki Mario
Posts: 285
Joined: Wed Feb 25, 2009 10:04 am
Contact:

Re: Creating KG-Chart for Mac

Post by Icelandic Hitman »

nintandrew wrote:Thanks, if you get it working, let me know. I can't believe it can work for me and not for you. That's nuts. Sorry.
No no no that is typical. Whats nuts is when you run the application it doesn't work. You don't change the elf/exe or any data at all, just run it again and it works. Thats nuts.
Thread Tracker for iPhone/iPod touch/iPad: http://touchscreencraft.wordpress.com
Etsy items for sales and charts : http://www.etsy.com/shop/touchscreencraft
DA : http://oziphantom.deviantart.com/

nintandrew
Rank 5 - Frog Mario
Rank 5 - Frog Mario
Posts: 209
Joined: Mon Jun 16, 2008 7:36 pm

Re: Creating KG-Chart for Mac

Post by nintandrew »

Thanks for doing that. I'll look it over and maybe get back to you in two years when I can understand it. ;) If it makes it work for others though, all the better. I was not expecting anyone to be into games, programming and needlework all together, but here are you guys. Thanks for all the help and tips, hopefully I'll get better with time.
Eyup

User avatar
Icelandic Hitman
Rank 6 - Tanooki Mario
Rank 6 - Tanooki Mario
Posts: 285
Joined: Wed Feb 25, 2009 10:04 am
Contact:

Re: Creating KG-Chart for Mac

Post by Icelandic Hitman »

Heh, I didn't change it that much, should be pretty straight forward, no fancy fancy stuff ( just 1 enum ). I guess we are all a pretty random bunch eh? No worries on the help or tips, if you have any obj-/c/++ questions just ask me, I'll be more than happy to help. Don't worry you will get better with time.
Thread Tracker for iPhone/iPod touch/iPad: http://touchscreencraft.wordpress.com
Etsy items for sales and charts : http://www.etsy.com/shop/touchscreencraft
DA : http://oziphantom.deviantart.com/

nintandrew
Rank 5 - Frog Mario
Rank 5 - Frog Mario
Posts: 209
Joined: Mon Jun 16, 2008 7:36 pm

Re: Creating KG-Chart for Mac

Post by nintandrew »

This is not the KG-Chart program, yet. Hitman, I finished the book on C I was reading, and enumerated data types were in the last chapter XD. During this last winter break, in-between semesters, I got back into programming and finished the book on C and got through another on Objective C, and am currently on one for Cocoa. What I have attached here is like a dumbed down version of the C program, but this has a real user interface! This app only displays/changes one color at a time, and you have to search by the code, with no option for searching by the name of the color. This has basically been a learning experiment, but I think it could be useful on its own too. I plan to redo this, and use a table view of the colors, where you can filter it by what you have/don't have and so on, but that might be a ways out. After, I would probably get started on the chart maker. For the "main event", I have been trying to figure out how to get charts from KG to work with it, like I thought if I could take the PDF of a chart and somehow compare the symbols with the key that's included in those patterns and have the program build the chart from that, but I don't think that that'll work. Anyway, here's the program, and I really hope it works for everyone (with a mac):
Attachments
FlossChecker.app.zip
(84.89 KiB) Downloaded 231 times
Eyup

User avatar
Icelandic Hitman
Rank 6 - Tanooki Mario
Rank 6 - Tanooki Mario
Posts: 285
Joined: Wed Feb 25, 2009 10:04 am
Contact:

Re: Creating KG-Chart for Mac

Post by Icelandic Hitman »

Wow, last chapter >.< I guess diferent books do it in different orders.

I have been looking at the sth file format, its some custom compressed binary format. The only way to decoded it would be to get the programmer to agree and give you the format description.. sigh...
PDF presents another problem, its a closed format as well. If it is made with text and colour blocks it is possible to get the chart from it, but it just wraps a pixel map then you would need to OCR it and that is a major pain as well >.<

:banghead:
Thread Tracker for iPhone/iPod touch/iPad: http://touchscreencraft.wordpress.com
Etsy items for sales and charts : http://www.etsy.com/shop/touchscreencraft
DA : http://oziphantom.deviantart.com/

nintandrew
Rank 5 - Frog Mario
Rank 5 - Frog Mario
Posts: 209
Joined: Mon Jun 16, 2008 7:36 pm

Re: Creating KG-Chart for Mac

Post by nintandrew »

That sucks about the PDF and .sth files, Hitman. The maker of KG-Chart said he might make a pattern viewer for mac though, so maybe I can email him later on and ask for the description. Anyways, I got this ver. 2.0 "done" much faster than I thought it would be. I say "done" because it's still rather meh. There isn't any sorting, and the last two columns in the program kind of annoy me, but It's now functional as a 2.0. For the last two columns, I tried to use checkboxes, but after a day and a half of frustration and getting no where, I decided to change it to what it is now. (Question I asked on StackOverflow, along with old code found here: http://stackoverflow.com/questions/4866423/ ... ablecolumn).
So, this program isn't up to what I wanted it to be, but it works, and I'm happy enough with it. If anyone wants me to go back and get it to sort and stuff, I can make it like ver. 2.5 or something, but right now, I want to finish this book on Cocoa and get started on the the actual chart maker. Using Nintendophile's new logo for the boards, I even made a logo for the program (Nintendophile/Johloh, if this isn't okay, I can destroy them):
Image

I also saw the thread on the floss list program for the iPhone costing $2, that sort of thing always seems to happen to ideas I have.

Anywho, if someone wants me to polish this more, let me know, otherwise, I'm off to learn about cool stuff like bindings and making separate windows for stuff.
Thanks :cool:
Attachments
FlossCheckerv2.0.zip
(97.58 KiB) Downloaded 227 times
Eyup

nintandrew
Rank 5 - Frog Mario
Rank 5 - Frog Mario
Posts: 209
Joined: Mon Jun 16, 2008 7:36 pm

Re: Creating KG-Chart for Mac

Post by nintandrew »

Alright, sorry for the double post, but it's been... 5 months!? Anyway... I actually started on my final stretch about a week or two ago, and have read quite a bit to get here. So I am stuck now and I asked for help over on stackoverflow, (link here: http://stackoverflow.com/questions/6590 ... 03#6591003) but I haven't gotten anywhere, so I thought I would try here. If you guys know any Cocoa programming or someone who does, I could sure use the help.

Here's a pic of what I have so far. The buttons don't do anything, the pencil icon isn't mine and it needs to be resized and you can read about the problems in the link, but it's progress. Also, thanks to ScienceDad for the webpage for RGB colors for DMC.

Image
Eyup

User avatar
Icelandic Hitman
Rank 6 - Tanooki Mario
Rank 6 - Tanooki Mario
Posts: 285
Joined: Wed Feb 25, 2009 10:04 am
Contact:

Re: Creating KG-Chart for Mac

Post by Icelandic Hitman »

Yo

I think the problems you are having is related the the fact OSX doesn't clear the area you are drawing on first, InvalidateRect on Windows does, Scroll Views don't. So the reason you are getting the darker lines is because you are drawing over the old lines and the lines are being AA, and hence blending. The reason you are getting 'trails' is because you are drawing the black square where the new stitch should be, but nothing is drawing a white square where the old one was. The easiest way to remedy this, is at the start of the drawRect call, draw a solid white ( or whatever the fabric colour is ;) ) rectangle the size of the passed in rect and then draw the updated view within it. Re-enable the coping of the scroll view as it will make the app faster.

You really should write your Gird line rendering code to only draw the lines that are shown by the passed in rect, but that is an optimisation not mission critical.

A 2D array is the fastest data structure you can get, the trick will be only draw the stitches that fall within the rect, not all of them.

Hope that helps
Thread Tracker for iPhone/iPod touch/iPad: http://touchscreencraft.wordpress.com
Etsy items for sales and charts : http://www.etsy.com/shop/touchscreencraft
DA : http://oziphantom.deviantart.com/

nintandrew
Rank 5 - Frog Mario
Rank 5 - Frog Mario
Posts: 209
Joined: Mon Jun 16, 2008 7:36 pm

Re: Creating KG-Chart for Mac

Post by nintandrew »

Thanks Hitman! But I just solved it last night. :D

I think it turned out to be the -lockFocus call in -drawStitch, which locks the focus to the window of the receiver according to the documentation. Anyway, I made a new project and discovered that the scrollview will retain bezier paths drawn in the -drawRect method, but only the last path to be drawn. So I made another new project and reused most of the code from the first and rearranged it (I think I need to study more on MVC designing) so -drawStitch makes the rect to be filled, but it's actually drawn in -drawRect by appending the stitches path with the rect. So all the stitches drawn of one colorx are one bezier path and then I decided that I need to use another path for the symbols.

I tried adding a '+' glyph to the stitching path, but it was antialiased, and when I set it to alias it was uneven, so I'm just drawing the symbols with -moveToPoint and -lineToPoint.

My next step that I want to start when I have to time from my Chemistry is to make NSDictionaries for the colors and then have those dictionaries in one large dictionary. The multiple dictionaries would be the properties for each color like the RGB value and symbol (I'm not sure if this would allow symbol changing by the user, but that's not very high on my priority list right now).

So, yeah, I wanted to post an update today to say that for the three years I've been registered, I've never seen my name under the Birthdays list and have wanted to, but when I looked today, I realized I hadn't even given my b-day date. But it's there now, so all is good.
Eyup

Post Reply