code flow (order) here

Started by
20 comments, last by fir 9 years, 11 months ago

Im tryin to alanyze this simple source

https://github.com/tinkerlog/PongTime/blob/master/src/com/tinkerlog/android/pongtime/PongTime.java

https://github.com/tinkerlog/PongTime/blob/master/src/com/tinkerlog/android/pongtime/PongTimeView.java

there are just to files,

I would like to get know about code flow here - I mean the ordering in which will call the functions there (especially startup and then game loop) But though it is simple source i got a trouble to understand this code order flow

could someone maybe help wutht that and list the list of things and order in which tey would be presumably executed?

- if so tnx

Advertisement

Everything starts at PongTimeView() constructor.

The file contains a private class PongThread which extends Thread. In other words it works like a thread.

Upon creation of the surface the game is going to rendered on, the thread is started.

The thread is initialized in a PAUSED state.

This state is used in a state machine in updateTime() which determines what to do based on the games "state," a term that is up to the programmer to define.

The states are PAUSED PLAY STOPPED and a few others, including an invalid state.

Thread::run(): the game loop

lock canvas

1. time is taken

2. updatePhysics() is called, which calls updateTime()

3. draw to canvas

unlock canvas

repeat... until surface is destroyed, or public function setRunning(false)

That's an Android game, so you must know a little about Android activities to fully understand it: http://developer.android.com/guide/components/activities.html#Lifecycle

When an Android application is started an Activity is created and started. Which activity is created is specified in the Android manifest (an XML file that's not present there, but should be present in any Android project), but only one of those is an activity so it's the only option for a starting point. I'm not saying that Android assumes that, the activity MUST be specified in the manifest.

Activities on Android have a lot of functions that you have to implement for different events. When the activity is created "onCreate" is called, so that's the first thing that will be executed. That function creates all the components that will be displayed.

Android has "layouts" to control the screens, those layouts are composed by components and one component is SurfaceView. When the layout is loaded in the onCreate method, a PongTimeVIew component is created and the method surfaceCreated from PongTimeView is called and the thread is started.

Allright, tnx, I will answet to it toomorrow (need a bit of thinking)

but i could ask yet one thing i do not understand in meantime

in oncreate in activity there is a line (80)

pongView = (PongTimeView)findViewById(R.id.pongview);

I do not understand this, pongView is an large object how you can just assign something to a large object - i would understand assigning something to a field of that but to a whole object - what is assigned here? does such assigning overvrite the left side, I mean previous state of an object - this line is strange to me; Can someone answer that?

(soory for font thuis changed after paste and i see no easy trick to change font tonormal - ok I changed that)

Allright, tnx, I will answet to it toomorrow (need a bit of thinking)

but i could ask yet one thing i do not understand in meantime

in oncreate in activity there is a line (80)

pongView = (PongTimeView)findViewById(R.id.pongview);

I do not understand this, pongView is an large object how you can just assign something to a large object - i would understand assigning something to a field of that but to a whole object - what is assigned here? does such assigning overvrite the left side, I mean previous state of an object - this line is strange to me; Can someone answer that?

(soory for font thuis changed after paste and i see no easy trick to change font tonormal)

pongView will store a reference to the view returned by findViewById. (R.id.pongview is a resource identifier so the view is most likely generated from an xml file)

if pongView allready had a reference to some other object the old reference would be dropped. (in this case that code should only run once when the activity starts).

consider this example

MyClass a = new MyClass();
MyClass b = a;
//Now a and b reference the same object.
a.someMember = 5;
System.out.println(b.someMember) //This will print 5 since a and b are the same object.
 
b = new MyClass(); //now b will reference a new object, a is unchanged.
b.someMember = 10;
System.out.println(a.someMember) //this will print 5 since a and b are different objects now
 
a = b.Clone(); // this creates a copy of b and assigns its reference to a, the original object referenced by a is no longer referenced by anything and will be garbage collected eventually. (Most standard library classes implement the Clonable interface)
 
System.out.println(a.someMember) //this prints 10
b.someMember = 15;
System.out.println(a.someMember) //this still prints 10 since a and b are different objects now.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

Allright, tnx, I will answet to it toomorrow (need a bit of thinking)

but i could ask yet one thing i do not understand in meantime

in oncreate in activity there is a line (80)

pongView = (PongTimeView)findViewById(R.id.pongview);

I do not understand this, pongView is an large object how you can just assign something to a large object - i would understand assigning something to a field of that but to a whole object - what is assigned here? does such assigning overvrite the left side, I mean previous state of an object - this line is strange to me; Can someone answer that?

(soory for font thuis changed after paste and i see no easy trick to change font tonormal - ok I changed that)

There's no such thing as "large" objects, you can say an object is bigger than another if it has more attributes and methods than the other, but that's not something you can write in code (in you don't need too). Maybe by "large object" you mean an actual object (an instance of a class) and by field you mean an attribute of a primitive type (non-object) like int, float, etc...

Maybe you know about OOP using another language like C++, where you must write if a variable is the object or is a pointer to the object and you tought the left side of that line was the actual object. In Java you always handle pointers to variables (when an attribute's type is a class) or a value (when the attribute's type is a primitive type). A pointer to a variable is just a variable that olds the address in memory where that object is. If you write attributeName = newValue Java knows that if attributeName is of a class type then it must copy the address of newValue (not the object) into attributeName, and if attributeName is of a primitive type then it must copy the value.

In that particular case, the attribute pongView of that Activity will always point at a PongTimeView object, but you must initialize the value before using it, otherwise that pointer will point anywhere and that's not good. When the layout of the activity is created an instance of PongViewTime is created by Android, when you call "findViewById" Android returns the address of the PongTimeView object it created before, so that line is only storing the address value of that object into the pointer, which is an attribute of the Activity. That line will never be called again (onCreate is called only when the Activity is created) so there's no way you can be overwriting a previous state of that object or losing anything.

as to code flow i will answer yet a bit later coz this is harder and more confusing, but here as to this assigning i can say what confuses me

PongTimeView is user defined large object has its own constructor and own user defined fields.. How it is possible to assign some thing taken from the resource into large user defined object - those two must be quite different type objects

also if you make such objects by such ssigning, what with constructor isnt it unused then at all? If it was used previously and setted some fields does the assigning of other object not overwrite it? (I would understand assigning something to a field of object - it can leave other user defined fields unchanged .. but assigning to whole object? Do it work also in such way? I mean it assigns only some base pointer and lefts other fields untached?)

This is very strange

Also it is very hard to see for me where constructor is clled - is here this assign come before constructor and then constructor is called later or constructor is called previously this assignment later? Im much confuzed (abit angry, maybe as seen :c)

You shouldn't be reading Android code, it looks like you have some really basic concepts to learn first... SimonForsman and I explained that in that line you mention the only thing that's being copied is the address in memory of the PongTimeView object. Nothing more is copied, there's only one instance of PongTimeView in the whole program and in that line the activity is asking android for the address of that instance so it can access it later.

The constructor of PongTimeView isn't called directly in the code, Android creates that object when it creates the layout of the PongTime activity. The line "setContentView(R.layout.main);" (line 78 of PongTime) triggers a function that at some point creates an instance of PongTimeView and that's where the constructor is called. Android handles that... as I said before, there are files missing in that project. The layout is defined by the programmer in a .layout file. That layout has an ID and that ID is "main". Inside the layout there's only one element, a PongTimeView component that extends SurfaceView (a valid component type for android's layouts) and the ID of that component is "pongview".

One of the many thing Android does automatically is creating a class called "R" that has all the id's you define in your code, so R.layout.main is the id of the layout, and R.id.pongview is the id of the PongTimeView object. setContentView searchs for the layout file that describes the "main" layout and creats the layout object, and that creation also creates the components inside that layout. The next line, findViewByID searchs the layout components until it finds the one with id "pongview" and returns the address.

Android does a lot of things automatically that make it harder for you to understand what's happening, search for a Pong or other simple game that's not for Android and that doesn't use a big framework. Look for a game done only with Java and Swing and it should be easier to understand, but it will be hard too if you don't know basic concepts. And also, look for a tutorial, it's a lot easier to understand everything when the code is explained step by step... the 2do page in a quick google search pointed me to this one: http://staticvoidgames.com/tutorials/swing/SwingPong.jsp

I recommend you put that PongTime code aside for a while and go see some more didactic code where you can see concepts clearly. Once you are confortable reading code that only uses standard Java stuff then you can start learning about how Android handle stuff (again, with more didactic examples) and after that you'll fully understand what's happening in that game. The missing files from that project and not knowing about Android programming are not helping you, you're wasting time trying to understand too much new stuffs at the same time.

there is something like folder layout there and some file caled main.xml

https://github.com/tinkerlog/PongTime/blob/master/res/layout/main.xml

isnt it the layout thing?

Well this thing with hidden constructor was really strange imo.

Ps i treat it as a kind of logical puzzle/riddle, will try some of the tutorial but as a last resort, now i just want to try to understand it with to much external study (if possible)

Oh, right, there was all the code. I only saw those 2 files on the first post and tought the rest was missing, and I was confused about the .layout extension. That's the layout file (main.xml), there's also the AndroidManifest.xml one in the root of the project that defines that PongTime activity is the activity that will be used when launching the app (action MAIN, category LAUNCHER).

This topic is closed to new replies.

Advertisement