Anyone want to help me with ZDL3 real quick?

Stuff you want Bio to see. [Home]

Moderator: BioHazard

Locked
User avatar
BioHazard
Posts: 408
Joined: Mon Jul 25, 2005 10:02
Location: Middle of California where there is no air.
Contact:

Anyone want to help me with ZDL3 real quick?

Post by BioHazard »

I got bored with PHP and decided to pick up and work on ZDL3 for a while.
I coded up his block and I'm wondering if anyone can try to shrink it a bit.
  • All code must be C compatible.
  • You may use seperate functions if you wish.
  • Try to keep it memory efficent as well.
With that in mind, let's see what you can do with this!:
[spoiler]

Code: Select all

LRESULT CALLBACK ConfigProc(HWND Dlg,UINT Msg,WPARAM wParam,LPARAM lParam){
	int i=0;
	PORT *tmpp;
	IWAD *tmpi;
	switch(Msg){
		case WM_COMMAND:switch(LOWORD(wParam)){
		// Buttons
			// Add/Deleting buttons
			case BTN_ADD:
				i=SendMessage(GetDlgItem(Dlg,LST_PORT),LB_GETCOUNT,0,0);
				port[i]=malloc(sizeof(PORT));
				strcpy(port[i]->name,"New Port");
				strcpy(port[i]->path,"(NONE)");
				SendMessage(GetDlgItem(Dlg,LST_PORT),LB_ADDSTRING,0,(LPARAM)port[i]->name);
			break;
			case BTN_IADD:
				i=SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_GETCOUNT,0,0);
				iwad[i]=malloc(sizeof(IWAD));
				strcpy(iwad[i]->name,"New IWAD");
				strcpy(iwad[i]->path,"(NONE)");
				SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_ADDSTRING,0,(LPARAM)iwad[i]->name);
			break;
			case BTN_REM:
				if((i=SendMessage(GetDlgItem(Dlg,LST_PORT),LB_GETCURSEL,0,0))!=LB_ERR){
					free(port[i]);
					SendMessage(GetDlgItem(Dlg,LST_PORT),LB_DELETESTRING,i,0);
					for(i;port[i]&&i<MAX_PORT;i++){port[i]=port[i+1];}
					SendMessage(GetDlgItem(Dlg,EDT_NAME),WM_SETTEXT,0,(LPARAM)"");
					SendMessage(GetDlgItem(Dlg,EDT_FILE),WM_SETTEXT,0,(LPARAM)"");
				}
			break;
			case BTN_IREM:
				if((i=SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_GETCURSEL,0,0))!=LB_ERR){
					free(iwad[i]);
					SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_DELETESTRING,i,0);
					for(i;iwad[i]&&i<MAX_IWAD;i++){iwad[i]=iwad[i+1];}
					SendMessage(GetDlgItem(Dlg,EDT_INAME),WM_SETTEXT,0,(LPARAM)"");
					SendMessage(GetDlgItem(Dlg,EDT_IFILE),WM_SETTEXT,0,(LPARAM)"");
				}
			break;
			// Moving buttons
			case BTN_UP: // Move port up
				if((i=SendMessage(GetDlgItem(Dlg,LST_PORT),LB_GETCURSEL,0,0))!=LB_ERR&&i!=0){
					// Move the entry in the window
					SendMessage(GetDlgItem(Dlg,LST_PORT),LB_INSERTSTRING,i-1,(LPARAM)port[i]->name);
					SendMessage(GetDlgItem(Dlg,LST_PORT),LB_DELETESTRING,i+1,0);
					// Move the memory item
					tmpp = port[i-1];
					port[i-1] = port[i];
					port[i] = tmpp;
					SendMessage(GetDlgItem(Dlg,LST_PORT),LB_SETCURSEL,i-1,0);
				}
			break;
			case BTN_IUP: // Move IWAD up
				if((i=SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_GETCURSEL,0,0))!=LB_ERR&&i!=0){
					// Move the entry in the window
					SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_INSERTSTRING,i-1,(LPARAM)iwad[i]->name);
					SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_DELETESTRING,i+1,0);
					// Move the memory item
					tmpi = iwad[i-1];
					iwad[i-1] = iwad[i];
					iwad[i] = tmpi;
					SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_SETCURSEL,i-1,0);
				}
			break;
			case BTN_DWN: // Move port down
				i=SendMessage(GetDlgItem(Dlg,LST_PORT),LB_GETCURSEL,0,0);
				if((i=SendMessage(GetDlgItem(Dlg,LST_PORT),LB_GETCURSEL,0,0))!=LB_ERR&&i!=SendMessage(GetDlgItem(Dlg,LST_PORT),LB_GETCOUNT,0,0)-1){
					// Move the entry in the window
					SendMessage(GetDlgItem(Dlg,LST_PORT),LB_INSERTSTRING,i+2,(LPARAM)port[i]->name);
					SendMessage(GetDlgItem(Dlg,LST_PORT),LB_DELETESTRING,i,0);
					// Move the memory item
					tmpp = port[i+1];
					port[i+1] = port[i];
					port[i] = tmpp;
					SendMessage(GetDlgItem(Dlg,LST_PORT),LB_SETCURSEL,i+1,0);
				}
			break;
			case BTN_IDWN: // Move IWAD down
				i=SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_GETCURSEL,0,0);
				if((i=SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_GETCURSEL,0,0))!=LB_ERR&&i!=SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_GETCOUNT,0,0)-1){
					// Move the entry in the window
					SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_INSERTSTRING,i+2,(LPARAM)iwad[i]->name);
					SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_DELETESTRING,i,0);
					// Move the memory item
					tmpi = iwad[i+1];
					iwad[i+1] = iwad[i];
					iwad[i] = tmpi;
					SendMessage(GetDlgItem(Dlg,LST_IWAD),LB_SETCURSEL,i+1,0);
				}
			break;
		}break;
	}return 0;
}
[/spoiler]
User avatar
BioHazard
Posts: 408
Joined: Mon Jul 25, 2005 10:02
Location: Middle of California where there is no air.
Contact:

Post by BioHazard »

Well let's see, I worked on it for a bit and managed to reduce it to this:
[spoiler]

Code: Select all

LRESULT CALLBACK ConfigProc(HWND Dlg,UINT Msg,WPARAM wParam,LPARAM lParam){
	int i=0,q=0;
	PORT *tmpp;
	IWAD *tmpi;
	switch(Msg){
		case WM_COMMAND:switch(LOWORD(wParam)){
		// Buttons
			case BTN_ADD:case BTN_IADD: // Add
				q=(LOWORD(wParam)==BTN_ADD)?(1):(0);
				i=SendMessage(GetDlgItem(Dlg,(q)?(LST_PORT):(LST_IWAD)),LB_GETCOUNT,0,0);
				if(q){port[i]=malloc(sizeof(PORT));}
				else{iwad[i]=malloc(sizeof(IWAD));}
				strcpy((q)?(port[i]->name):(iwad[i]->name),"New");
				strcpy((q)?(port[i]->path):(iwad[i]->path),"(NONE)");
				SendMessage(GetDlgItem(Dlg,(q)?(LST_PORT):(LST_IWAD)),LB_ADDSTRING,0,(q)?((LPARAM)port[i]->name):((LPARAM)iwad[i]->name));
			break;
			case BTN_REM:case BTN_IREM: // Delete
				q=(LOWORD(wParam)==BTN_REM)?(1):(0);
				if((i=SendMessage(GetDlgItem(Dlg,(q)?(LST_PORT):(LST_IWAD)),LB_GETCURSEL,0,0))!=LB_ERR){
					if(q){free(port[i]);}else{free(iwad[i]);}
					SendMessage(GetDlgItem(Dlg,(q)?(LST_PORT):(LST_IWAD)),LB_DELETESTRING,i,0);
					if(q){for(i;port[i]&&i<MAX_PORT;i++){port[i]=port[i+1];}}
					else{for(i;iwad[i]&&i<MAX_IWAD;i++){iwad[i]=iwad[i+1];}}
					SendMessage(GetDlgItem(Dlg,(q)?(EDT_NAME):(EDT_INAME)),WM_SETTEXT,0,(LPARAM)"");
					SendMessage(GetDlgItem(Dlg,(q)?(EDT_FILE):(EDT_IFILE)),WM_SETTEXT,0,(LPARAM)"");
				}
			break;
			case BTN_UP:case BTN_IUP: // Move up
				q=(LOWORD(wParam)==BTN_UP)?(LST_PORT):(LST_IWAD);
				if((i=SendMessage(GetDlgItem(Dlg,q),LB_GETCURSEL,0,0))!=LB_ERR&&i!=0){
					SendMessage(GetDlgItem(Dlg,q),LB_INSERTSTRING,i-1,(q==LST_PORT)?((LPARAM)port[i]->name):((LPARAM)iwad[i]->name));
					SendMessage(GetDlgItem(Dlg,q),LB_DELETESTRING,i+1,0);
					SendMessage(GetDlgItem(Dlg,q),LB_SETCURSEL,i-1,0);
					if(q==LST_PORT){tmpp=port[i-1];port[i-1]=port[i];port[i]=tmpp;}
					else{tmpi=iwad[i-1];iwad[i-1]=iwad[i];iwad[i]=tmpi;}
				}
			break;
			case BTN_DWN:case BTN_IDWN: // Move down
				q=(LOWORD(wParam)==BTN_DWN)?(LST_PORT):(LST_IWAD);
				i=SendMessage(GetDlgItem(Dlg,q),LB_GETCURSEL,0,0);
				if((i=SendMessage(GetDlgItem(Dlg,q),LB_GETCURSEL,0,0))!=LB_ERR&&i!=SendMessage(GetDlgItem(Dlg,q),LB_GETCOUNT,0,0)-1){
					SendMessage(GetDlgItem(Dlg,q),LB_INSERTSTRING,i+2,(q==LST_PORT)?((LPARAM)port[i]->name):((LPARAM)iwad[i]->name));
					SendMessage(GetDlgItem(Dlg,q),LB_DELETESTRING,i,0);
					SendMessage(GetDlgItem(Dlg,q),LB_SETCURSEL,i+1,0);
					if(q==LST_PORT){tmpp = port[i+1];port[i+1] = port[i];port[i] = tmpp;}
					else{tmpi = iwad[i+1];iwad[i+1] = iwad[i];iwad[i] = tmpi;}
				}
			break;
		}break;
	}return 0;
}
[/spoiler]
So who wants to tell me that is unreadable? ;)
User avatar
DoomRater
Posts: 397
Joined: Tue Jul 19, 2005 4:14
Location: Programmer's Room, talking to Will Harvey
Contact:

Post by DoomRater »

I'm drawing similarities between this and VB at the moment. I'm getting the major difference:

VB handles the interrupt calling for you so you don't have to, basically, so I wouldn't have to write the switch(Msg) and the various cases.

Thank God learning a new programming language is nothing like learning a new verbal language. I can actually make sense out of it.
User avatar
BioHazard
Posts: 408
Joined: Mon Jul 25, 2005 10:02
Location: Middle of California where there is no air.
Contact:

Post by BioHazard »

Heh, I noticed that the PORT and IWAD structs were the same so I made them one type (ITEM), this made the code able to be written like this:
[spoiler]

Code: Select all

LRESULT CALLBACK ConfigProc(HWND dlg,UINT msg,WPARAM wp,LPARAM lp){
	int i=0,q=0;
	ITEM *tmpi=0;
	ITEM **item=0;
	switch(msg){
		case WM_COMMAND:switch(HIWORD(wp)){
			case BN_CLICKED:switch(LOWORD(wp)){
				case BTN_ADD:case BTN_IADD:
					item=(LOWORD(wp)==BTN_ADD)?(port):(iwad);
					arg1=q=(LOWORD(wp)==BTN_ADD)?(LST_PORT):(LST_IWAD);
					arg2=-1; // -1 == BTN_ADD pressed
					arg3=i=(short)SendMessage(GetDlgItem(dlg,q),LB_GETCOUNT,0,0);
					DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(DLG_FILE),dlg,FileProc);
					// If a new entry was added, add it to the listbox
					if(item[i]){SendMessage(GetDlgItem(dlg,q),LB_ADDSTRING,0,(LPARAM)item[i]->name);}
					arg1=arg2=arg3=0; // Reset the temp vars
				break;
				case BTN_REM:case BTN_IREM: // Delete button
					item=(LOWORD(wp)==BTN_REM)?(port):(iwad);
					q=(LOWORD(wp)==BTN_REM)?(LST_PORT):(LST_IWAD);
					if((i=SendMessage(GetDlgItem(dlg,q),LB_GETCURSEL,0,0))!=LB_ERR){
						free(item[i]);
						SendMessage(GetDlgItem(dlg,q),LB_DELETESTRING,i,0);
						SendMessage(GetDlgItem(dlg,q),LB_SETCURSEL,i,0);
						for(i;item[i]&&i<MAX_ITEM;i++){item[i]=item[i+1];}
					}
				break;
				case BTN_UP:case BTN_IUP: // Move up
					item=(LOWORD(wp)==BTN_UP)?(port):(iwad);
					q=(LOWORD(wp)==BTN_UP)?(LST_PORT):(LST_IWAD);
					if((i=SendMessage(GetDlgItem(dlg,q),LB_GETCURSEL,0,0))!=LB_ERR&&i!=0){
						// Move the entry in the window
						SendMessage(GetDlgItem(dlg,q),LB_INSERTSTRING,i-1,(LPARAM)item[i]->name);
						SendMessage(GetDlgItem(dlg,q),LB_DELETESTRING,i+1,0);
						SendMessage(GetDlgItem(dlg,q),LB_SETCURSEL,i-1,0);
						// Move the item
						tmpi=item[i-1];item[i-1]=item[i];item[i]=tmpi;
					}
				break;
				case BTN_DWN:case BTN_IDWN: // Move down
					item=(LOWORD(wp)==BTN_DWN)?(port):(iwad);
					q=(LOWORD(wp)==BTN_DWN)?(LST_PORT):(LST_IWAD);
					i=SendMessage(GetDlgItem(dlg,q),LB_GETCURSEL,0,0);
					if((i=SendMessage(GetDlgItem(dlg,q),LB_GETCURSEL,0,0))!=LB_ERR&&i!=SendMessage(GetDlgItem(dlg,q),LB_GETCOUNT,0,0)-1){
						// Move the entry in the window
						SendMessage(GetDlgItem(dlg,q),LB_INSERTSTRING,i+2,(LPARAM)item[i]->name);
						SendMessage(GetDlgItem(dlg,q),LB_DELETESTRING,i,0);
						SendMessage(GetDlgItem(dlg,q),LB_SETCURSEL,i+1,0);
						// Move the item
						tmpi=item[i+1];item[i+1]=item[i];item[i]=tmpi;
					}
				break;
			}break;
		}break;
	}return 0;
}
[/spoiler]
Locked

Return to “Bio's Site-Place”