Monday, October 31, 2011

buried

The big conference I go to for work every year is coming up. It's not necessarily taking up all my spare time but the prep is wearing me out enough to keep me from programming.

I spent some time with K&R this week. My hope is that I'll have a good "getting to know getchar()" post soon. It's important to know for the middle sections of K&R since it's how you get the data you apply the advanced data structures to. Anyways it's complicated and I want to write it all out so that I never have to think about it again.

EDIT: Oh yea and Battlefield 3 has not been  letting me do other stuff that isn't Battlefield 3.

Saturday, October 22, 2011

pre progress report and more of the gap

I'm having a good time learning Objective-C so far. I had thought that it was some really crazy new way of thinking about classes and program organization and it would be hard to learn. It actually is a crazy new way of thinking about classes (in implementation, not general idea of OOP) and it's a stickler for organization but doesn't rigidly enforce it. Even a smallish Obj-C program can be a half-dozen files since every class needs a .h and .m file (not NEEDS, just prefers).

Over the past year I've read the occasional tutorial or sample code and left with my head swimming. The really goofy stuff I saw makes sense now, and the goofy stuff is actually pretty neat and makes for faster implementation.

The future post about my progress will go over what I've learned so far and my thoughts on the language in general.

There are lots of C related stuff I'm kicking myself over not getting around to doing, like the more advanced data structures. Cocoa/Foundation has its own easy to use versions of linked lists and dynamic arrays and whatnot (I'm sure C++ has this in the STL somewheres) but I'll feel bad about using them before I implement it myself at least once so that I really know what it's all about. Hashing, too. I want to learn about it since it comes up from time to time.

My mind has been on what I call "the gap" again. The first gap every new programmer crosses is the gap between variables and getting those variables presented to the user (typically via printf or equivalent). The second gap (for me) was going from console-land to a modern style GUI. It took me until I was 28 to get to that point! My third gap (perhaps third and fourth) are 2d and 3d graphics. I was reading something about Perlin noise and the code for generating that noise was written out and the author had some nice pretty pictures and I realized something - the code he had in his article was just the math to generate noise. There were no bits of code for turning those functions into images. How did he get from one to the other? So that's been on my mind and I'm worried there is some big obvious thing I'm missing or that I'm making it out to be more complicated than it really is. From what I could tell the functions for generating the noise accepted an x and y position and spit out a value between -1 and 1. Scanning through all the x and y positions in the image, getting back this number, and shading/coloring it appropriately is probably all that was going on.

Therefore I promise this: when I figure out how to do anything important I will always in full present the code used to generate the end result. The entire reason this journal exists is to present what I'm learning in a clear enough way such that the 15 year old me would have a much easier time learning about those concepts. I did a much better job of this when I was learning the basics of C (especially pointers) but I've slacked a bit.

Friday, October 21, 2011

so far so good



This is part of an "apply what you know" part of the Objective-C Programming:TBNRG book. It's supposed to help solidify using object methods (and reading the Apple documentation on string methods I guess).

Thursday, October 20, 2011

Come and Git it.

I started reading the first few chapters of Advanced Mac OS X Programming: The Big Nerd Ranch Guide and so far it's been super enlightening. There are a lot of things I don't understand about C that aren't really about C at all and are compiler or platform specific, like what's available to you in the pre-processor. The book has a great super clear explanation of how to use all those fancy #define and #ifdef bits work. That's the kind of stuff that I'm not abundantly familiar with that's preventing me from moving on beyond being a beginner C programmer to an intermediate C programmer. I had no idea you could set #define information in the GCC command line, either. Where do people learn this stuff? I feel super fortunate that I came across this book because I'd still be in the dark otherwise.

I thumbed through Programming in Objective-C and realized I can probably skip the first nine or so chapters since it covers basic C stuff like data types and control structures. The juicy bits are how classes are implemented.

Someone on Reddit posted a link to a site that has the full text (.pdf) of Michael Abrash's Graphics Programming Black Book. I didn't realize that it was legit released for free online. I just picked a chapter and page at random and had my mind blown. He's a really good writer.

This morning I made a GitHub account and did the quick tutorial on making a repository and pushing a file up. At this point I'm just following directions because I have no idea what any of the stuff I'm typing in does. I'm kind of irked because it has a great walk-through on creating a repository, making a new file, and commiting it, but it doesn't say what to do after you edit the file. I can't tell if the commands in the tutorial were only for new files or for any file. I don't plan on using Git or any source control for every quick program I write (unless I feel like I need the practice) but it will help make the case for trying to work on more long term projects.

EDIT: Also a good (free) read: C Elements of Style


Sunday, October 16, 2011

Instance methods and class methods

I have a somewhat decent grasp of common OOP terms like class, method, inheritance, polymorphism, etc. Something that I keep seeing in my casual reading about Objective-C is that some methods in class definitions have + preceding them, and others -.

The + means the method is a class method, and the - means it's an instance method. An example in the book I'm reading today (Objective-C Programming: The Big Nerd Ranch Guide) illustrated one difference between the two.

The NSDate class has a method called date which returns a pointer to an instance of the NSDate class.

NSDate *sometime = [NSDate date];

date is a class method.

There is another method called timeIntervalSince1970 which returns a double (seconds since 1970). This is an instance variable, so you call it on the instance.

double timepassed = [sometime timeIntervalSince1970];

It's not legit to say:

double timepassed = [NSDate timeIntervalSince1970];

and you'll get warned that no such class method exists.

So there's OOP with Objective-C lesson one. There are class methods and instance methods. I tried doing a few different things, like:

NSDate *now = [NSDate];

but I got an error "Expected identifier". Is an identifier a class method that has to be run? Is this the concept as a constructor in C++? I peeked into NSDate.h and found a few other class methods alongside date. I found an instance method called init and thought I should try it too

NSDate *now = [NSDate init];

and this didn't give me an error. So that's confusing - both init and timeIntervalSince1970 are instance methods but the former works and the latter fails. Going back to NSdate.h I found that timeIntervalSince1970 isn't quite in the same place as init is, so maybe it has different rules. Further confusing the matter is that init is of type id which is a generic pointer to an Obj-C object, and timeIntervalSince1970 is a double.

Well shoot. I mean this post to be about how I learned something and now I'm just more confused.

Friday, October 14, 2011

Compiling GTK 3.0 programs in CodeBlocks

I spent some free time installing Ubuntu 11.10 in a VirtualBox session hosted by OS X. The trials involved with getting that to work may be the subject of another post. The punchline? VirtualBox and Unity 3d don't get along. Install the host drivers, update, then log in with Unity 2d selected. Doing those first two steps is tricky when the desktop windows don't redraw unless you go to the desktop switcher. Wowie. This was true in 11.04 and 11.10 beta 2.

My motivation for moving to 11.10 from 10.10 was so that I could get a handle on GTK 3.0. A few weekends ago I really beat myself into a pulp trying to make a basic "hello world" sort of program using Cairo in GTK and failed. A lot of the issues I had were with the basic initialization and packing a Cairo window into a GTK container. I also couldn't hash out the relationship between Cairo and GTK pixbuffs. That functionality seems to have changed a lot in GTK 3.0 and I want to try again.

I use CodeBlocks out of habit. Eclipse has a weird project organization that rubbed me the wrong way, and Anjuta was just plain hard to use. I don't know of any other popular IDEs in Linux (emacs and vim worry me).

Installing CodeBlocks and Synaptic Package Manager (to get the GTK 3.0 development files) was easy enough through the Ubuntu Software Center. No problems there, although I was very surprised that Synaptic wasn't pre-installed like in 10.10.

Expecting the default GTK program (the code that is generated when you start a new GTK project) to compile the first time was naive of me (well... it worked for GTK 2.0). The errors were about not being able to find the header files. I went error to error and added the paths to the headers in the compiler search options (Project->Build Options->Search directories->Compiler tab). Once all the missing files errors went away I got a bunch of new errors about how all the functions weren't defined. This basically means the libraries it needed to link against couldn't be found. Hooray.

I sat and thought about it and realized that if I were to compile this in the terminal I'd use pkg-config. pkg-config is a tool that looks at some different file (.pc files?) for all the compiler flags you'd need to use a certain package, like GTK.

If I type into the terminal: pkg-config --cflags gtk+-3.0

I get this out:

-pthread -DGSEAL_ENABLE -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/gtk-3.0

Similarly, if I type in: pkg-config --cflags gtk+-3.0

I get this:

-pthread -lgtk-3 -lgdk-3 -latk-1.0 -lcairo-gobject -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lrt -lglib-2.0 

GCC has this neat feature (or maybe it's a Linux thing?) where you can embed commands with text output into the GCC options when you run it if you surround the command with these: ''

So the compile command would look like this:

gcc program.c -o program`pkg-config --cflags --libs gtk+-2.0`

And you'd get all of those extra instructions into the gcc command. Pretty neat, right?

Ok, so I got real excited and found precedent for using pkg-config in CodeBlocks via StackOverflow in a question very similar to my own:

http://stackoverflow.com/questions/5921460/how-to-setup-gtk-to-develop-with-codeblocks-on-ubuntu-linux

However, when I did this (add the pkg-config lines to the compiler/linker "Other options") the error message I got said there was no input file. It's the same error you get if you just run gcc in the terminal.

As a last resort I simply copied the output of "pkg-config --cflags gtk+-2.0" into the compiler "Other options" and the output of "pkg-config --libs gtk+-2.0" into the linker "Other options" and now everything works just fine. This seems like a bit of a hack but I can't argue with the results.

I'm glad I finally have a functional development environment, but this raises a lot of questions. Why did it automagically work with GTK 2.0 in Ubuntu 10.10? Why did implementing the solution in the StackOverflow thread not work? Is this how everyone else has to do it?

This is a pretty strong argument in favor of avoiding IDEs for everything but editing. I should either learn Make or create a shell script that runs gcc with the pkg-config lines that takes in the .c file as an argument.

New stuff

I've been using Blogger since 2003 (prior to the Google purchase of the service). It's about time I start using some of the more advanced features and widgets and stuff. I've added an incomplete "about" page, a label list, and a Twitter account.

I'm going to try and keep the posts here to important things (excluding this post of course) that are worth the time it takes to write out. The purpose of this journal is to write out things I've learned in such a way that a teenage version of myself (the one that tried and failed at learning C the first time) could understand it.

I debated whether or not to end this journal and start a new one since I've pretty much stopped only focusing on C and have moved on to new languages like Python and soon Objective-C, as well as new frameworks like GTK and soon Cocoa. If I were to move to a new one I'd probably try to get my own domain name and a more complex journaling software like Wordpress. Maybe someday, but this serves my needs for now.

Wednesday, October 12, 2011

Throwing books at the problem

I ordered a few books on Objective-C and Cocoa programming. My reasons are as follows.

1) One of my motivations for learning to program was to try and eventually build some good mobile application ideas I had. I own an iPhone and bought a Macbook to learn C/Python/etc on so I might as well go whole hog.

2) OSX uses GCC and shares a great deal of common UNIX programming paradigms. One of the books I picked up has a good section on GCC and GDB which will carry over into Linux.

3) I like working in Linux and using GTK, but it's getting cumbersome to always do it in a virtual environment. I've also gotten far enough along in GTK to realize that I could easily spend the rest of my hobby time for years learning the deeper bits and I'd rather spend that time learning something that will help point number 1.

4) I already have a few C++ books collecting dust on my shelf, and honestly what I've seen of C++ programming frightens me. I don't want to learn Java. I feel like I could quickly get competent enough at both in a short amount of time if I really had to.

This are the most important points:

5) My life has always been about the niche skills or uncommon elements. I play drums, I have a neuroscience degree, I have pet rats, I do tech support for a niche product, and so on. Objective-C is an uncommon (relative to C++/Java) language typically learned for a specific niche market. Why fight it?

6) Objective-C is a strict super-set of C. C++ claims to be sometimes, but there are so many exceptions. Nothing I learned will have been lost or need to be checked against special rules.

7) I started out trying to learn C, but that goal has evolved to simply learning to program.

8) I'm not in this to make a career move. That would be an unreasonable expectation. If I meant to switch careers I'd double down on Python and Java since those are more marketable skills. That being said, a little Python knowledge has gone a long way at the office.

I'm giving myself the winter to explore this. From now until the end of February. I'd like to be at least as competent at Cocoa (native OS X framework) as I was at GDK/GTK, and also have a solid handle on Objective-C syntax common object oriented patterns (hopefully my work in Python will have helped).

Monday, October 10, 2011

deep

http://www.slideshare.net/olvemaudal/deep-c

Long, but pretty sweet dialog on what a deeper meaning of c/c++ means.

Sunday, October 9, 2011

the week off

I took the week off from coding, but not from planning the next stage. Since GTK3 seems to be the way to go I spent time installing Ubuntu 11.04 and 11.10 beta2 in a virtualbox instance. The new "unity" interface (I think it's called) doesn't play nice but I'm working on it.

The whole week wasn't code-free. Out of necessity I did some Python stuff. My inability to parse Python's documentation is holding me back.

Monday, October 3, 2011

no go

I burned a lot of time over the weekend on the Cairo/GTK problem.

My assumption was that putting a Cairo-drawable window in a GTK window would be like initializing and attaching any other widget (like a button, form, text box, etc). The examples and tutorials I could find only did this very simply - it initialized a main GTK window and then put the Cairo window in that. What I wanted to do was not much more complex - I wanted a button in addition to the Cairo window. What I figured would happen is I could have a main Window, embed a VBox which is a container that holds other widgets in a vertical arrangement, and then embed the button and Cairo window into that. What emerged however is that there is no Cairo window. You have to have a different container (unknown) and then somehow set it as drawable (unknown) and hook a callback for doing the drawing with the "expose-event" (why?). Every example had all the drawing functions in the "expose-event" callback - what if I wanted to alter the drawing when I press a button? Do I have to artificially kick that event from the point of view of the drawable window?

I came to the conclusion late Sunday that this was too much work, not well documented, and nobody online has written up a detailed next-step tutorial for practical applications of Cairo in a GTK program.

But THEN I remembered something - I'm using GTK2 because that's what Ubuntu 10.10 supports out of the box. GTK3 changed up how it interacts with Cairo. My brief investigation revealed that the way GTK2 initialized and updated Cairo-based windows completely changed in GTK3. Not only is everything I painfully learned all wrong now, it also doesn't seem to be as painful to do.

So that's how I wasted a weekend where I had more free time than usual on a wild obsolete goose chase. I don't know if trying again next weekend is worth it - there are no tutorials that I can find online for GTK3 and Cairo, and too much of the official documentation assumes that you knew how the old system works. Lots of "instead of doing it like this, do this", but not knowing how the first way of doing it worked is crippling.

I also discovered that the relationship with GTK and GDK is not what I thought it was. I had assumed GTK was the GUI aspect and GDK was everything else (data types, etc), but I found that GDK is a whole layer of drawing libraries between GTK and X. GLib is the "everything else".

I genuinely took away nothing meaningful from the weekend. It might as well have never happened.

EDIT: I wish I had seen this yesterday: http://zetcode.com/tutorials/gtktutorial/gtkevents/

The "timer" example has exactly what I needed to know. I think the missing info is that there are GDK (not GTK) specific functions for interfacing Cairo drawing functionality with GTK widgets. In the example a GTK drawing area (which I'm sure doesn't exist in GTK3) is embedded in a window - this is in contrast to drawing directly on the window which I didn't want to do.