Animation Curves and Tweening; Understanding Animation Curves

These equations are by Robert Penner, you can find a document here that explains this in much greater detail:
www.robertpenner.com/easing/

The correct linear tween function looks like this:

float linear_tween(float t, float start, float end)
{
    if (t > 1.0f) return end;
    return t * end + (1.0f - t) * start;
}

t is the time since the start of animation started and it has to be between 0 and 1, start and end are the values that you want to interpolate between them…

To understand this equation you can so something very simple, assume start is 1 and end is 20, then at timestep 0 you will have:

0 * end + (1 – 0) * start => start

At time step 1 which is the end of your animation you get:

1 * end + (1 – 1) * start => end

And all the values between these are going to be interpolated linearly.

Now quadratic ease in function is where the changes in interpolated value are smaller in the beginning and larger at the end, how can we achieve this? We can do it by faking the time steps, if you we could somehow make it so that the time steps would look smaller to the linear tween function at the start of the animation and larger at the end, then we would achieve this, that way, in the beginning, the time steps look smaller so the function will change the value by just a little bit but at the end, the time steps look larger so the function will change the value by a larger amount.

The way to do this is very easy, the fact that the time step number is clamped to between 0 and 1 will help us in this case and this is the solution:

float quadratic_easein(float t, float start, float end)
{
    if (t > 1.0f) return end;
    return linear_tween(t * t, start, end);
}

When we multiply the time step by itself the numbers that are less than one get smaller, for example if you are at the time step 1/10th of a second:

1/10 * 1/10 => 1/100
2/10 * 2/10 => 4/100 (3/100 change)
3/10 * 3/10 => 9/100 (5/100 change)
4/10 * 4/10 => 16/100 (7/100 change)

8/10 * 8/10 => 64/100
9/10 * 9/10 => 81/100 (17/100 change)

Now let’s look at quadratic_easeout:

float quadratic_easeout(float t, float start, float end)
{
    if (t > 1.0f) return end;
    return linear_tween(2 * t - t * t, start, end);
}

Deriving 2*t – t*t is very easy knowing the fact above; to do this we need to reverse what happens above so we can do something:

t = 1 – t;

Now 1/10 becomes 9/10 so when we do:

t = t * t

The numbers are going to change more at the beginning of the time step but the problem is that now we converted 1/10 to 9/10 and t*t will result in 81/100 and that’s the end of our time step and the animation will play backwards, to fix it we need to do:

t = 1 – t

One more time so then 81/100 becomes 19/100, now let’s simplify this:

1 – t => 1 – t * t => 1 – ((1 – t) * (1 – t)) => 1 – (1 – t – t + t * t) => 2 * t – t * t

I hope this helps spacer

Animation Curves and Tweening; Understanding Animation Curves
Comments (1)   Filed under: C Programming,C/C++,Mac,Objective-C,OpenGL   Posted by: Codehead

JavaScript: setTimeout doesn’t work in IE

The only way this could happen is if you try to send extra arguments, so you can’t do: setTimeout(‘func()’, 1000, param); you have to manage with something like: setTimeout(‘func(“‘ + arg + ‘”)’, 1000);

JavaScript: setTimeout doesn’t work in IE
Comments (0)   Filed under: Annoying Stuff,JavaScript,Web Development   Posted by: Codehead

iPhone: libpng error: CgBI: unknown critical chunk

If you get this error when trying your app on the device, you have PNG compression on in Xcode, in Xcode 4 goto “Build Settings” search for PNG in the search box and turn off PNG compression…

iPhone: libpng error: CgBI: unknown critical chunk
Comments (1)   Filed under: C Programming,iPhone SDK,Mac Programming,Xcode   Posted by: Hamid

Cocoa App Doesn’t Show Up In Activity Monitor

This was happening because I accidentally deleted “Bundle Display Name” from my info.plist…..

Cocoa App Doesn’t Show Up In Activity Monitor
Comments (0)   Filed under: Mac Programming,Objective-C,Xcode   Posted by: Hamid

Popen+ a Bidirectional Popen Implementation With Ability to Access PID and Kill/Terminate Processes

On Github:
https://github.com/Codingrecipes/Popen+

This is a little popen implementation which will make it easier to kill processes and will give you access to the PID of the process, I don’t know why they didn’t do this in popen but I’m sure they had good reasons because those guys are way too smart…

popen_plus.h:

/*
 ** Author: Hamid Alipour codingrecipes.com twitter.com/code_head
 ** SQLite style license:
 ** 
 ** 2001 September 15
 **
 ** The author disclaims copyright to this source code.  In place of
 ** a legal notice, here is a blessing:
 **
 **    May you do good and not evil.
 **    May you find forgiveness for yourself and forgive others.
 **    May you share freely, never taking more than you give.
 **/
 
#ifndef POPEN_PLUS_H
#define POPEN_PLUS_H
 
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <paths.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
 
#define READ 0
#define WRITE 1
 
struct popen_plus_process {
    pthread_mutex_t mutex;
    pid_t pid;
    FILE *read_fp;
    FILE *write_fp;
};
 
struct popen_plus_process *popen_plus(const char *command);
int popen_plus_close(struct popen_plus_process *process);
int popen_plus_kill(struct popen_plus_process *process);
int popen_plus_kill_by_id(int process_id);
int popen_plus_terminate(struct popen_plus_process *process);
int popen_plus_terminate_with_id(int process_id);
 
#endif

popen_plus.c:

/*
 ** Author: Hamid Alipour codingrecipes.com twitter.com/code_head
 ** SQLite style license:
 ** 
 ** 2001 September 15
 **
 ** The author disclaims copyright to this source code.  In place of
 ** a legal notice, here is a blessing:
 **
 **    May you do good and not evil.
 **    May you find forgiveness for yourself and forgive others.
 **    May you share freely, never taking more than you give.
 **/
 
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <paths.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include "popen_plus.h"
 
struct popen_plus_process *popen_plus(const char *command)
{
    int inpipe[2];
    int outpipe[2];
    char *argv[4];
    struct popen_plus_process *process = malloc(sizeof(struct popen_plus_process));
 
    if (!process)
        goto error_out;
 
    if (pipe(inpipe) != 0)
        goto clean_process_out;
 
    if (pipe(outpipe) != 0)
        goto clean_inpipe_out;
 
    process->read_fp = fdopen(outpipe[READ], "r");
    if (!process->read_fp)
        goto clean_outpipe_out;
 
    process->write_fp = fdopen(inpipe[WRITE], "w");
    if (!process->write_fp)
        goto clean_read_fp_out;
 
    if (pthread_mutex_init(&process->mutex, NULL) != 0)
        goto clean_write_fp_out;
 
    process->pid = fork();
    if (process->pid == -1)
        goto clean_mutex_out;
 
    if (process->pid == 0) {
        close(outpipe[READ]);
        close(inpipe[WRITE]);
 
        if (inpipe[READ] != STDIN_FILENO) {
            dup2(inpipe[READ], STDIN_FILENO);
            close(inpipe[READ]);
        }
 
        if (outpipe[WRITE] != STDOUT_FILENO) {
            dup2(outpipe[WRITE], STDOUT_FILENO);
            close(outpipe[WRITE]);
        }
 
        argv[0] = "sh";
        argv[1] = "-c";
        argv[2] = (char *) command;
        argv[3] = NULL;
 
        execv(_PATH_BSHELL, argv);
        exit(127);
    }
 
    close(outpipe[WRITE]);
    close(inpipe[READ]);
 
    return process;
 
clean_mutex_out:
    pthread_mutex_destroy(&process->mutex);
 
clean_write_fp_out:
    fclose(process->write_fp);
 
clean_read_fp_out:
    fclose(process->read_fp);
 
clean_outpipe_out:
    close(outpipe[READ]);
    close(outpipe[WRITE]);
 
clean_inpipe_out:
    close(inpipe[READ]);
    close(inpipe[WRITE]);
 
clean_process_out:
    free(process);
 
error_out:
    return NULL;
}
 
int popen_plus_close(struct popen_plus_process *process)
{
    int pstat;
    pid_t pid;
 
    /**
     * If someone else destrys this mutex, then this call will fail and we know
     * that another thread already cleaned up the process so we can safely return
     * and since we are destroying this mutex bellow then we don't need to unlock
     * it...
     */
    if (pthread_mutex_lock(&process->mutex) != 0)
        return 0;
 
    if (process->pid != -1) {
        do {
            pid = waitpid(process->pid, &pstat, 0);
        } while (pid == -1 && errno == EINTR);
    }
 
    if (process->read_fp)
        fclose(process->read_fp);
 
    if (process->write_fp)
        fclose(process->write_fp);
 
    pthread_mutex_destroy(&process->mutex);
 
    free(process);
 
    return (pid == -1 ? -1 : pstat);
}
 
int popen_plus_kill(struct popen_plus_process *process)
{
    char command[64];
 
    sprintf(command, "kill -9 %d", process->pid);
    system(command);
 
    return 0;
}
 
int popen_plus_kill_by_id(int process_id)
{
    char command[64];
 
    sprintf(command, "kill -9 %d", process_id);
    system(command);
 
    return 0;
}
 
int popen_plus_terminate(struct popen_plus_process *process)
{
    char command[64];
 
    sprintf(command, "kill -TERM %d", process->pid);
    system(command);
 
    return 0;
}
 
int popen_plus_terminate_with_id(int process_id)
{
    char command[64];
 
    sprintf(command, "kill -TERM %d", process_id);
    system(command);
 
    return 0;
}

This code was inspired by many sources and I think I perfected it, although you might find bugs or issues and if you do, please let me know if the comments section.

You can use it like so:

struct popen_plus_process *process = popen_plus("ls -l");
if (!process) {
     /* Failed do something and return or exit */
     return -1;
}
 
int MAX_BUFFER = 256;
char buffer[256];
 
while (!feof(process->read_fp))
     if (fgets(buffer, MAX_BUFFER, process->read_fp) != NULL)
          printf("%s\n", buffer);
 
popen_plus_close(process);

To kill a process:

popen_plus_kill(process);
popen_plus_close(process);

Terminate is similar to kill…

I hope this helps someone!

spacer

Popen+ a Bidirectional Popen Implementation With Ability to Access PID and Kill/Terminate Processes
Comments (0)   Filed under: C Programming,Linux Programming,Mac Programming,Unix Programming   Posted by: Hamid

NSNotificationCenter Is Not Thread Safe

If you have to send notifications from secondary threads, then you might have noticed that your app crashes randomly and it’s unstable, the way to get around this is to have a method that sends the notification and use a second method to call that method using performSelectorOnMainThread like so:

- (void)doPostStatusNotification:(NSString *)message {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SOME_ID" object:self userInfo:[NSDictionary dictionaryWithObject:message forKey:@"message"]];
}
 
- (void)postStatusNotification:(NSString *)message {
    [self performSelectorOnMainThread:@selector(doPostStatusNotification:) withObject:message waitUntilDone:NO];
}

And, never call the first method, always use the second one…

NSNotificationCenter Is Not Thread Safe
Comments (0)   Filed under: Cocoa,Mac Programming,Objective-C   Posted by: Hamid

A Nicer Way Of Updating NSTableView Without A NSTimer

I used to use timers to update my table views, but you know that the timer runs in the same thread that you create it and it attaches to the same runloop so that’s not very nice and it’s silly to make a thread for it so naturally you might end up making a timer with longer time intervals and your table view will have delays.

There is a nicer way of handling this, you can use NSKeyValueObserving and all you have to do in is to import two headers on top of your model:

#import <Foundation/NSKeyValueCoding.h>
#import <Foundation/NSKeyValueObserving.h>

Then, when you load your model items attach observers to each one, like so:

[directory addObserver:self forKeyPath:@"some-property-of-your-model-object-like-status" options:NSKeyValueObservingOptionNew context:nil];

You can add and observer to as many properties as you want.

Then implement the delegate method in your controller:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (![the-table-view needsDisplay])
        [the-table-view setNeedsDisplay:YES];
}

If you add new rows or remove them, you will need to send a reloadData message to your table view.

That’s it, much simpler than caveman way of doing it and updates are almost instant…

A Nicer Way Of Updating NSTableView Without A NSTimer
Comments (0)   Filed under: Cocoa,Mac Programming,Objective-C   Posted by: Hamid

NSTableView reloadData Hangs And Doesn’t Work

This happened to me and everything was working, the table was linked up properly to it’s delegate and datasource but I discovered that I was calling reloadData too many times, so this problem could be caused by two things:

1 – If you call reloadData too many times, it messes up some internal thing and it stops responding.
2 – It has a mechanism in place that detects this issue and kind of stops responding.

Now, to try to fix this, you must know what reloadData does, it simply sets a flag in the table view:

[the-table-view setNeedsDisplay:YES];

And if you did any reading or research you know that this doesn’t guaranty a refresh – which leads me to believe the second point above might be true – of the view so to fix my issue I tried:

if (![the-table-view needsDisplay])
        [the-table-view setNeedsDisplay:YES];

You must call reloadData only when you add new/or remove data to/from the dataset.

This seems to be working…

NSTableView reloadData Hangs And Doesn’t Work
Comments (1)   Filed under: Cocoa,Mac Programming,Objective-C   Posted by: Hamid

SQLite And The Problem Of Storing Long Long Integers

Recently, I was trying to store a very large integer value in a SQLite column, it didn’t matter wether I used INTEGER or UNSIGNED BIG INT, SQLite rounded it for me and I was left with an integer value that wasn’t even close to what I needed.

So one solution was to just use a VARCHAR or TEXT or event a BLOB and insert the number with quotes around

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.