How to refresh the screen in OpenGL after something is printed? - android

I have a couple of if-statements and if one of them is true it will print a character on the screen.
char d = 'a';
char c = 'b';
if(A == TRUE) //see comments below
{
Print(200,200,d);
}
if(B == TRUE)
{
Print(200,200,c);
}
void Print(int x, int y, char u)
{
glRasterPos2f(x,y);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, u);
}
The problem is that if A is true, it will print 'a' on the screen correctly, but if afterwards B is true and A not anymore it will print 'b' on the screen while the character 'a' is still there. How can I solve this so that only one character is on the screen. Any help would be appreciated.
Big thanks to datenwolf. The most important thing is to put the glutPostRedisplay in the right place. I placed it at the end of my void Print(..) function. I just looked where I should place it and for me this works now (it was just a small adjustment).

OpenGL's design is built on the premise, that the while window is redrawn if anything needs a change. Normally all drawing operations should be done from only a central display function. Changes that need to be reflected in what's visible on the screen should be dealt with by preparing the list of things to be drawn and then issue a full redraw event.
Update
Actually you have a completely different problem
In the statement if(A = TRUE) two things are happening:
A gets assigned the value TRUE
the result of that assignment (which is TRUE) is tested for truth, which it is and hence will always pass.
The same goes for if(B = TRUE).
The right operator to use is == (two equal signs in a row). With most compilers you can enable a warning and turn that into an error, that assignments are not allowed in a branching statement. A stylistic approach is to write tests for equality as
if( TRUE == A )
which avoids accidents like yours. However in your case you can simply write
if( A )
without any comparison operators at all. And I strongly suggest you do that.

Related

Unexplained Snapping Behaviour In A Programmatically Scrolled "Lazy List""

I stumbled upon this issue while trying to solve a case. To summarise, the patient needed complete control over the 'speed' of the scroll. Now, Android's sensible defaults have the item-scroll follow the user's finger at all times, but apparently an un-shared condition/requirement of this patient lead them to want the user scrolling further than the item actually scrolls. So, I suggested, as originally proposed by David in his solution, to disable the user's direct control over the lazy list, and manually scroll it by observing events from a touch detection callback, like the ones provided by the pointerInput(...) Modifier. Hence, the implementation:
#Composable
fun LazyStack(sdd: Int = 1) { // Scroll-Distance-Divisor
val lazyStackState = rememberLazyListState()
val lazyStackScope = rememberCoroutineScope()
LazyColumn(
modifier = Modifier.pointerInput(Unit) {
detectVerticalDragGestures { _, dragAmount ->
lazyStackScope.launch {
lazyStackState.scrollBy(-dragAmount / sdd)
}
}
},
state = lazyStackState,
userScrollEnabled = false
) {
items((0..500).map { LoremIpsum(randomInt(5)) }) {
Text(text = it.values.joinToString(" "))
}
}
}
Now, the treatment actually worked, except a .. tiny side-effect, apparently.
You see, the list actually scrolls perfectly as intended for the most part. For example, if I pass the sdd parameter as 2, the scroll distance is reduced to half its original value. But, the side-effect is "snapping".
Apparently while scrolling smoothly through the list, there are certain "points" on both sides of which, the list "snaps", WHILE THE USER HAS THIER INPUT POINTER IN USE (a finger, a mouse, or any other pointers, are actively "holding" the list). Now, this is a point, which, if you cross in either direction, the list snaps, and apparently by the exact same value every single time.
The list above uses Lorem Ipsum as a convention, but perhaps a numbered list would help the diagnose the problem.
So, a synopsis of the symptoms:
There seem to be certain "points" in the list, occurring at inconsistent intervals/positions throughout the list/screen which tend to make the list instantly scroll through a lot of pixels. Once discovered, the points are consistently present and can be used to re-produce the symptom unless recomposed. The value of the "snap" is apparently not returned to be a massive float when continuously logging the value returned by the scrollBy method. This makes the issue untraceable by regular diagnostic techniques, which is why we, the diagnosticians are here.
You have the history, you have the symptoms. Let the differential diagnosis begin.
The snapping behavior is caused by incorrectly used randomInt(5), within a composable. List generation should be inside the viewmodel then there is no regeneration of the list during scrolling and everything works perfectly.
Also using detectVerticalDragGestures does not work as smoothly as my original suggestion of using modfiier.scrollable(...) but maybe that's a desired effect.

Best way to capture a quickly incrementing/rotating value to confirm it passed a threshold

I have a variable whose value can change as frequently as the device frame-rate. Now, actually it is an angle, but perhaps can be thought of as a regularly incrementing value. What I wish, is to calculate how many 360 loops have been made. I have a Composable, which I have hooked up to this value. So the user is basically rotating an indicator, and I update the value based on the angle. Now, I already have a system put in place which performs a calculation on the indicator position and renders an angle ranging from 0 to 359 (well 360 actually, but non-inclusive). So, now if the user is spinning the indicator, I want to know when he actually hits the 0f Angle. I tried it with a conditional but this does not work if the user is going even at a reasonable speed. He must be incredibly slow in order to capture that value.
Here's a sample
val angle = ...//Already Got this Value
if(angle == 0f) { /*One cycle Complete*/ } // But this is not detected mostly
Now, remember that this is Compose, so we must take recompositions into account. I have tried with a list, by storing some of the latest values of the variable, but it just rotates too fast. All the values end up being the same, unless the size is crazy like 500 or so.
Can my approach be better? Any known way of accomplishing this without changing the approach. Basically any help would be appreciable.
This is what I ended up doing and it works at any given speed
var cycles by remember { mutableStateOf(0) } // Holds cycles state
val startCheck by mutableStateOf(angle in 300..360) //The Hot-Zone for Active Monitoring
LaunchedEffect(angle) { //This executes every time angle changes, i.e, at frame rate
if (startCheck) { //Only if indicator is in the hot zone
if (angle <300 ) cycles++ //Increment the Cycles
}
}

Cant check if my textview that inside of an array is visible or not in Kotlin

I have a 2D array (matrix) of Textviews called Board. Board has 16 Textviews in it, only one of them is invisible. In this given lines of code, I tried to find the invisible one between all the rest. For some reason, the line with the If condition collapses my app every time. I don't understand what my problem is, can someone help me?
P.S. Sorry for my English, it's not my native language.
Here is my code:
var i = 0
for (i in 0..4) {
var j = 0
for (j in 0..4) {
var tvtemp = board[i][j]
if (tvtemp.visibility == View.INVISIBLE) {
Toast.makeText(applicationContext,board[i][j].text, Toast.LENGTH_SHORT).show()
}
}
}
== operator is used to compare the data of two variables.
Please don’t misunderstand this equality operator with the Java == operator as both are different. == operator in Kotlin only compares the data or variables, whereas in Java or other languages == is generally used to compare the references.
Solution:
if(!tvtemp.isVisible){
....
}
You could try using the isShown method. It determines if the view is visible to the user. If a view's visibility is set to be visible, but it it crossed off the screen then isShown will equal false. If you have no scrollview and all views are fitted in the activity then this could work for you
if (!tvtemp.isShown) {
Toast.makeText(applicationContext,board[i][j].text, Toast.LENGTH_SHORT).show()
}

Does break statement inside if inside while finish "while loop" completely or only immediate "if"?

I have this code, that caused me error. The code goes through list and sees if any user is logged in, if it is logged in it counts which user it is - first or second.
When i placed that break statement inside if, the loop only found the first user and did not do anything about 2nd user - the entire loop was ending! How so? I thought the break only works for the immediate block it is placed within, i.e. if break is in if{}, it breaks out of this if and the code execution continues.
Does break statement end even the outside loops?
do {
if (userlogin){
if(how_many_logged_in == 0){
name1.setVisibility(View.INVISIBLE);
}
else if(how_many_logged_in == 1){
name2.setVisibility(View.INVISIBLE);
break ; //this is confusing - where does it break from?
}
}
} while (condition);
As per the java language specification, break ends abruptly the innermost enclosing switch, while, do, or for control structure. So, if/then/else are simply ignored.
As this says,break is used to leave most nearest loop,for example:
while(***){break;}
do{break;}while(***)
switch
for
,but doesn't work in statements like if.

Moving text field position on keyboard show in Lua for Android

I am using the SDK "Corona" to develop Android applications in Lua.
I was wondering if you could all assist me in the following problem I have been experiencing. I am in the process of designing an app with text fields towards the bottom of the screen, but I would like the text fields to change position once touched, so that the keyboard does not overlap them when it pops up.
As such, I created a listener to execute code which changes the Y position of 2 fields when either one is touched, but for some odd reason, the code is not working. If I place the code in a button event listener however, it seems to work fine. Please refer to my code:
----------------email textbox -------------
local textField = native.newTextField( display.contentCenterX, display.contentCenterY + 60, 200, 40 )
textField.placeholder = "Email"
textField.isEditable = true
--function to handle events
local function touchListener( event )
textField.y = display.contentCenterY - 100
textField2.y = display.contentCenterY - 50
end
textField:addEventListener( "touch", touchListener )
I thank you in advance for your help.
If I'm correct, since textFields are a native object (and not a display object), they do not handle "touch" event.
As such you will need to use the "userInput" event to trigger the move of the textField.
Here is an example of the listener I have used in the past for this case (without the code to move around):
local function fctFieldListener(oEvent)
local oTextField = oEvent.target
if "began" == oEvent.phase then
-- Move the input up if at the bottom
elseif "editing" == oEvent.phase then
elseif "submitted" == oEvent.phase then
native.setKeyboardFocus( nil )
elseif "ended" == oEvent.phase then
-- Move the input back at his original place if adjusted
end
end
And you add it to the textfield like this:
oTextField:addEventListener( 'userInput', fctFieldListener )
Also, I highly recommend that you put your native.newTextField in a specific display group that you will be moving around, instead of moving around the textField itself, makes things a lot easier.

Categories

Resources