Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


code flow (order) here


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
21 replies to this topic

#1 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 30 May 2014 - 01:38 PM

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



Sponsor:

#2 Kaptein   Prime Members   -  Reputation: 2178

Like
6Likes
Like

Posted 30 May 2014 - 02:26 PM

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)


Edited by Kaptein, 30 May 2014 - 02:32 PM.


#3 DiegoSLTS   Members   -  Reputation: 1869

Like
5Likes
Like

Posted 30 May 2014 - 02:54 PM

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.


Edited by DiegoSLTS, 30 May 2014 - 02:56 PM.


#4 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 30 May 2014 - 04:35 PM

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)

 


#5 SimonForsman   Crossbones+   -  Reputation: 6293

Like
3Likes
Like

Posted 30 May 2014 - 05:00 PM

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.

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!

#6 DiegoSLTS   Members   -  Reputation: 1869

Like
3Likes
Like

Posted 30 May 2014 - 08:22 PM

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.



#7 fir   Members   -  Reputation: -456

Like
-1Likes
Like

Posted 31 May 2014 - 01:50 AM

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)



#8 DiegoSLTS   Members   -  Reputation: 1869

Like
3Likes
Like

Posted 31 May 2014 - 09:24 AM

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.



#9 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 31 May 2014 - 09:55 AM

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)



#10 DiegoSLTS   Members   -  Reputation: 1869

Like
3Likes
Like

Posted 31 May 2014 - 10:20 AM

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).



#11 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 31 May 2014 - 10:59 AM

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).

 

 

 
ok, tnx for the info
in general this confusion with the hidden construction was maybe the bigger one, rest is maybe a bit less confusing
 
though
1) I do not udnerstand yet if this pongThread object nust be defined in the middle of the pongview? Wouldnt it be better to hold this in third file - when reading the source it brings the confusion.. are there some reasons to keep it in the middle ?
2) must be this surfaceHolder sent to both pongthread and pongview? if there is a call
 
     SurfaceHolder holder = getHolder();
 
does it mean that this getHolder is a global method or this is just a method of containing the view (probably the latter but there was also a littel confusion - if this is a method of the view why it is added to a view, this android oop java style of coding very weird)


#12 frob   Moderators   -  Reputation: 22692

Like
4Likes
Like

Posted 31 May 2014 - 04:40 PM

For #1, yes, it is generally a good practice to put each class in a separate file. The good practice is not always followed, especially for minor utilitarian items, or if it is a subclass that doesn't make sense on its own.

For #2, it could be anywhere. All you need to know it that it is within scope. It could be coming from that class, or from some other class, or be a global function.

For the previous post, it doesn't matter how large an object is, all that matters is that it implements the interface you are looking for. Since you are fairly familiar with C, this happens all the time in C as well, where all that matters is that it has the same layout, or that it is convertible. If your variable is for some specific type or interface, and if the object either matches or can be converted to that specific type or interface, it will work.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#13 DiegoSLTS   Members   -  Reputation: 1869

Like
3Likes
Like

Posted 31 May 2014 - 06:23 PM

You can define classes anywhere you want to, and even if there are "good practices" it never is so simple as "this is the best solution in any project". At the end the only right answer that always work is "it depends", since what is better also depends on a lot of stuff. In this case PongThread is inteded to be used only inside PongTimeView, so for the writer the best place to put that code was inside PongTimeView.

 

About getHolder... That's not an Android weird thing, you can call methods without specifing the object in java. In this case, "getHolder" is a method of SurfaceView, a class from the Android API:http://developer.android.com/reference/android/view/SurfaceView.html#getHolder%28%29

 

Since PongTimeView extends SurfaceView, that line is calling the getHolder() method of the PongTimeView object that was inherited from SurfaceView. If you write a call to a function without specifying and object, the compiler always uses this rule:

1 - If "this" has a method that matches that call (considering also inherited methods), the line is interpreted as this.method(). If not...

2 - If the class has a static method that matches, the line is interpreted as Class.method().

 

If those checks fail the code doesn't compile. In Java you don't have global functions, you have static methods on classes, but to access a static method from another class you must import that class and write NameOfThatClass.method().


Edited by DiegoSLTS, 31 May 2014 - 06:24 PM.


#14 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 01 June 2014 - 01:42 AM

You can define classes anywhere you want to, and even if there are "good practices" it never is so simple as "this is the best solution in any project". At the end the only right answer that always work is "it depends", since what is better also depends on a lot of stuff. In this case PongThread is inteded to be used only inside PongTimeView, so for the writer the best place to put that code was inside PongTimeView.

 

About getHolder... That's not an Android weird thing, you can call methods without specifing the object in java. In this case, "getHolder" is a method of SurfaceView, a class from the Android API:http://developer.android.com/reference/android/view/SurfaceView.html#getHolder%28%29

 

Since PongTimeView extends SurfaceView, that line is calling the getHolder() method of the PongTimeView object that was inherited from SurfaceView. If you write a call to a function without specifying and object, the compiler always uses this rule:

1 - If "this" has a method that matches that call (considering also inherited methods), the line is interpreted as this.method(). If not...

2 - If the class has a static method that matches, the line is interpreted as Class.method().

 

If those checks fail the code doesn't compile. In Java you don't have global functions, you have static methods on classes, but to access a static method from another class you must import that class and write NameOfThatClass.method().

 

Alright the second was a little confusing as it looked as something like global method, but now i see

 

as to embeddong pong thread - will it be much difference if pongthread would be defined in third java file?

 

for me it seems that almost nothing will change, (though I am not sure) because visibility depends more on passed pointers not where definition lies

 

If it would lay in seperate file

 

1) acces from pong thread to pong view would be harder?

or

2) from pong view to pong thread would be harder?

 

would there be a need to revrite something?

 

as it shows the embedded pong thread is also accessible

just they use

 

import com.tinkerlog.android.pongtime.PongTimeView.PongThread;

 

 

I am not really sure what change if its embedded (if some acceses are easier or harder and which one



#15 Olof Hedman   Crossbones+   -  Reputation: 2947

Like
2Likes
Like

Posted 01 June 2014 - 02:31 AM


I am not really sure what change if its embedded (if some acceses are easier or harder and which one

 

In this example, I think the difference would be minimal.

 

One advantage of inner classes is access to private fields of the containing class, so you could potentially write less lines of code.

 

I'm not a fan of huge inner classes like the one in the example though, makes the code harder to overview, that one maybe should have been a separate file IMO. 

If it only should be accessed within the view, it could be made private in the package, and you could only use it from within other classes in the same package. (though I don't think this is a huge issue?)

 

But for small classes that need access to private stuff of your class, and should not be used from outside, then inner classes are very useful.

 

As noted, its all convention and preference, nothing cut in stone.



#16 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 01 June 2014 - 03:03 AM

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.

Alright took a little time and now i see it ..now its much more clear to me..

 

There is yet a confusion what surface is - What view is I probably

understand - view is some rectangle area (like 'sticker', somethink like "virtual paper" cards)  where you could present graphics (and also manage this views just like paper cards )

- but what is surface - is this just a surface (something like pixel contents) of the view ? is view to surface 1-1 relation, I mean each view has its own surface and thats all?

Is here in that app only one fullscreen view that has one fullscreen surface and that all is here?

 

Yet one strange thing - it seem to me that i noticed that in some conf files this app is setted to android 3 and above, regardless that it is easy (basic GDI like, as i could call it with my win32 experience) and probably should run in older androids too (at home I gos old HTC wildfire phone, and it has 2.4 or 2.6 android 

(i dont remember), does it seem 1) that it will not work when trying to download it from android market 2)if i will just change in confs expected android to 2.4 and compile it myself to device then it will work? Why the man putted 3 here?



#17 Olof Hedman   Crossbones+   -  Reputation: 2947

Like
2Likes
Like

Posted 01 June 2014 - 04:58 AM


There is yet a confusion what surface is - What view is I probably
understand - view is some rectangle area (like 'sticker', somethink like "virtual paper" cards)  where you could present graphics (and also manage this views just like paper cards )
- but what is surface - is this just a surface (something like pixel contents) of the view ? is view to surface 1-1 relation, I mean each view has its own surface and thats all?
Is here in that app only one fullscreen view that has one fullscreen surface and that all is here?

 

Yes, you've got views right, they are higher level, used to layout content, and implement callbacks from buttons and such.

Surface is a handle to the buffer where the actual drawing takes place, and you draw to it using a canvas.

SurfaceView is a special view that help you do lowish level drawing onto the surface. (lower level would be direct pixel access).

 

Its not a strict 1:1 relationship, in a "standard" UI app you have a hierarchy of views defined in xml which layout pictures, text and buttons, and the framework makes sure to draw them into the surface for you, and you don't have to worry about the surface at all.

 


Yet one strange thing - it seem to me that i noticed that in some conf files this app is setted to android 3 and above

 

Probably not much thought behind it, you'd have to go through everything used to know for sure, but that example looks like it would run on a pretty low version indeed. You should be able to just change it. If you use eclipse, I think it will warn you if you try set the version too low.



#18 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 01 June 2014 - 11:58 AM

 


There is yet a confusion what surface is - What view is I probably
understand - view is some rectangle area (like 'sticker', somethink like "virtual paper" cards)  where you could present graphics (and also manage this views just like paper cards )
- but what is surface - is this just a surface (something like pixel contents) of the view ? is view to surface 1-1 relation, I mean each view has its own surface and thats all?
Is here in that app only one fullscreen view that has one fullscreen surface and that all is here?
 

 

Yes, you've got views right, they are higher level, used to layout content, and implement callbacks from buttons and such.

Surface is a handle to the buffer where the actual drawing takes place, and you draw to it using a canvas.

SurfaceView is a special view that help you do lowish level drawing onto the surface. (lower level would be direct pixel access).

 

Its not a strict 1:1 relationship, in a "standard" UI app you have a hierarchy of views defined in xml which layout pictures, text and buttons, and the framework makes sure to draw them into the surface for you, and you don't have to worry about the surface at all.

 

 

 

this more confuses than explains, is the surface 1:1 to a views or it is something more related to one physical screen device ?

I looked somewhat in android docs api but do not manage to understand it 



#19 Olof Hedman   Crossbones+   -  Reputation: 2947

Like
0Likes
Like

Posted 01 June 2014 - 01:06 PM

You could say it's one step away from the screen.  Its a buffer that is managed by the screen compositor, which is responsible for composing any visible surfaces onto the screen.

You won't be allowed closer to the screen then that, as an app smile.png

 

Usually you have only one fullscreen surface in your activity, and then all your views in that activity (as defined in the xml-file, or created and added in code) are drawn onto the same surface.

But you could have multiple layes of surfaces. Content like video and non-fullscreen gl will be drawn to their own surfaces and composed by the screen compositor.

 

SurfaceView is special, it the only view that has its own surface, and is the one you use if you need to implement views that need their own surface... Most views don't.

 

Hope that clears it up a bit


Edited by Olof Hedman, 01 June 2014 - 01:07 PM.


#20 DiegoSLTS   Members   -  Reputation: 1869

Like
0Likes
Like

Posted 01 June 2014 - 01:17 PM

SurfaceView extends View. All SurfaceView's instances are also Views, but more classes extend View, so it's not a 1:1. Any of the next classes are also Views:

 

A View is a rectangular component that can display something and detects user input. SurfaceView extends a View, so it does the same things and more. It means a SurfaceView is also a rectangular component that can display something and detects user input, but it provides methods to manipulate what is being drawn.

 

Do you know about inheritance? It looks like you get lost in basic concepts every time. I'll keep recomending you to learn more concepts and more about Android before reading that code (or from any other finished game). You can learn all those stuff a lot quickier with tutorials and guides, it doesn't make sense you try to guess things. Code is not a puzzle or a riddle, it's not a mathematical problem or a logic problem that you can "decipher" or "solve", it's more like a recipe that the computer follows.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS